响应者链相关问题

场景:

开发中事件的传递(响应者链)相关问题是不可避免的,本文是作者在开发中所遇到的问题和解决方案的集合,希望对每个读者有用
1、 在使用MVC架构模式中,不可避免的使view和controller分离于是我们不可避免的使用在视图中找控制器的操作,贴一段一直在用的代码:

//得到此view 所在的viewController
- (UIViewController*)viewController{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

2、 在ScrollView使用touchBegin方法是由于UIView的Touch方法被ScrollView拦截了,其解决方案如下:
创建UIScrollView的子类在子类中重写方法,保证事件的向下传递 。闲话不多说见代码

  #import <UIKit/UIKit.h>

  @interface UIScrollView (HAScrollView)
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
  @end

实现方法

#import "UIScrollView+HAScrollView.h"

@implementation UIScrollView (HAScrollView)
- (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];
}

在和在输入框中进行手写输入操作时候会出现闪退的问题,其原因是上述问题(原因没找到),可以通过jiejue

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,986评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,833评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,431评论 4 61
  • 转眼二宝5个月了。去年这个时候,她才在我肚子里安营扎寨,如今,已活脱脱的牢牢握在掌心里了,我欢喜自己又一次迎接新生...
    嘉卉1阅读 3,966评论 0 2
  • 黑夜是自卑者的温柔乡是作恶者的狂欢节 黑夜躲藏在黑夜掩饰身份,树立假墓碑掩饰表情,锻造新面具掩饰语言,反刍旧词语 ...
    河岛阅读 2,435评论 14 13

友情链接更多精彩内容