将目标设置为NSObject

时间:2013-12-01 07:30:28

标签: ios uigesturerecognizer target nsobject

我有NSObject创建UIImageView。我向UIImageView添加了手势识别器,但目标未设置为类本身。我想知道如何做到这一点。

//inside the init method of the NSObject:
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapRecognizer.numberOfTapsRequired = 2;
[theImage addGestureRecognizer:tapRecognizer];


//this method is directly beneath the init method of the NSObject, I want this 
//to be called when the image is double tapped
- (void)handleTap:(UITapGestureRecognizer*)recognizer
{

}

那么我需要设置initWithTarget:to?它不是self,因为这会导致应用程序崩溃,从而导致错误的访问错误

1 个答案:

答案 0 :(得分:1)

如果在NSObject类中设置目标,则该操作将不起作用。所以你必须在ViewController.m类中添加代码,然后它才能工作。