iOS自带手写键盘奔溃问题

很多时候为了满足需求,我们需要在UIScrollView添加UITextField,在处理键盘升降键盘事件的时候往往会写个UIScrollView的分类,代码

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {
    [[self nextResponder] touchesMoved:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [[self nextResponder] touchesEnded:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
}

后来我们的测试发现一个问题,当使用iOS自带手写键盘的时候输入第一个字变灰,再点击点击奔溃了。

调试了很久,我发现手写键盘在调用UIScrollView的这个分类的方法时,self的类型是UIKBCandidateCollectionView,具体是什么还没有去苹果官方文档查,先屏蔽了,代码

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (![self isMemberOfClass:[UIScrollView class]]) {
        
    } else {
        [[self nextResponder] touchesBegan:touches withEvent:event];
        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
            [super touchesBegan:touches withEvent:event];
        }
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self isMemberOfClass:[UIScrollView class]]) {
        
    } else {
        [[self nextResponder] touchesMoved:touches withEvent:event];
        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
            [super touchesMoved:touches withEvent:event];
        }
    }
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self isMemberOfClass:[UIScrollView class]]) {
        
    } else {
        [[self nextResponder] touchesEnded:touches withEvent:event];
        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
            [super touchesEnded:touches withEvent:event];
        }
    }
}

解决

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS 手写输入法奔溃,一种方法是常见的新建一个view监听点击手势,隐藏键盘,然后这个view最后加入到self...
    sll_阅读 1,529评论 0 0
  • 镜子喜欢听《西游记》,有时听王南叔叔说书,有时听凯叔讲的。经常手拿凯猴子一听半天,然后绘声绘色演讲出来,精彩处拿起...
    青蘭阅读 1,353评论 0 3
  • 听说他新的她很爱听笑话 于是把你以前为了哄他开心从四面八方收集来的笑话一个一个的讲给她听。你知道后笑了笑说这样也好...
    染月亮阅读 274评论 0 1
  • I:人要有时间打理自己,不用觉得羞耻。这要求你关注自己的精神状态、心理状态还有身体状态。首先选一个你觉得健康的事情...
    Sara_s阅读 134评论 0 1