我对UIGestureRecognizer有个奇怪的问题
我创建了一个类,在该类中声明手势识别器,并将self作为目标
-(id)initWithTextView:(UITextView*)theTextView withDelegate:(id<WordSelectionDelegate>)theDelegate
{
if (self = [super init])
{
delegate = theDelegate;
textView = theTextView;
// init long press gesture to detect pressing on text elements
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressFromSender:)];
[textView addGestureRecognizer:longPressGesture];
}
return self;
}
But the trick is when i actually make a long press gesture i have next error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableAttributedString handleLongPressFromSender:]: unrecognized selector sent to instance 0x76227b0'
Why does the messages to self goes to String???发布于 2012-10-07 15:04:03
顺便说一句,问题无疑是拥有handleLongPressFromSender实例方法的对象(即您正在使用initWithTextView初始化的对象)在调用UILongPressGestureRecognizer时超出了范围。您需要检查该对象的范围。
例如,假设这个类的名称是MyTextViewHandler,那么假设您为一些视图控制器设置了一个viewDidLoad,其内容如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// do a bunch of initialization
MyTextViewHandler *textViewDelegate = [[MyTextViewHandler alloc] initWithTextView:self.textview withDelegate:self];
}如果您在ARC项目中这样做,您将得到您描述的崩溃(因为textViewDelegate对象是viewDidLoad的本地对象,在该方法的末尾将超出范围)。如果使此委托处理程序类成为视图控制器的实例变量(或属性),则此问题就会消失。
https://stackoverflow.com/questions/11864840
复制相似问题