首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIGestureRecognizer目标问题

UIGestureRecognizer目标问题
EN

Stack Overflow用户
提问于 2012-08-08 12:46:19
回答 1查看 1.1K关注 0票数 1

我对UIGestureRecognizer有个奇怪的问题

我创建了一个类,在该类中声明手势识别器,并将self作为目标

代码语言:javascript
复制
-(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???
EN

回答 1

Stack Overflow用户

发布于 2012-10-07 15:04:03

顺便说一句,问题无疑是拥有handleLongPressFromSender实例方法的对象(即您正在使用initWithTextView初始化的对象)在调用UILongPressGestureRecognizer时超出了范围。您需要检查该对象的范围。

例如,假设这个类的名称是MyTextViewHandler,那么假设您为一些视图控制器设置了一个viewDidLoad,其内容如下:

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];

    // do a bunch of initialization

    MyTextViewHandler *textViewDelegate = [[MyTextViewHandler alloc] initWithTextView:self.textview withDelegate:self];
}

如果您在ARC项目中这样做,您将得到您描述的崩溃(因为textViewDelegate对象是viewDidLoad的本地对象,在该方法的末尾将超出范围)。如果使此委托处理程序类成为视图控制器的实例变量(或属性),则此问题就会消失。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11864840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档