2016-06-17 7 views
-5

imageViewにGestureRecognizerを追加し、clickActionでブロックを使用します。Object-Cでブロックを使用すると、「スレッド1:EXC_BAD_ACCESS(コード= 1、アドレス= 0x0)」を取得します。

しかし、私はそれを実行したとき、私はブロックにこのエラーが表示されます。

スレッド1:EXC_BAD_ACCESS(コード= 1、アドレス= 0x0の)

#import "blockImage.h" 

@implementation blockImage 

-(instancetype)init{ 
if (self = [super init]) { 
     self = [[blockImage alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; 
     self.tag = 10; 
     self.backgroundColor = [UIColor redColor]; 
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapActionWithBolck:)]; 
     [self addGestureRecognizer:tap]; 

         } 
return self; 
} 


-(void)tapActionWithBolck:(void(^)(NSInteger idx))completion{ 

    completion(10); 
    } 
@end 

#import "ViewController.h" 
#import "blockImage.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 

blockImage *img = [[blockImage alloc] init]; 
[self.view addSubview:img]; 

} 

@end 

答えて

1

UITapGestureRecognizerでこのようなメソッドを使用することはできません

-(void)tapActionWithBolck:(void(^)(NSInteger idx))completion;

それはあなたがタップでブロックを使用する場合は、初期化中に(変数に保管しなければならないこと

-(void)tapAction:(UIGestureRecognizer *)sender;

または

-(void)tapAction;

ようにする必要があります例えば、それをメソッドで呼び出すよりも。しかし、サイクルを留意してください。

関連する問題