场景:
开发中事件的传递(响应者链)相关问题是不可避免的,本文是作者在开发中所遇到的问题和解决方案的集合,希望对每个读者有用
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