我有自定义的UIViewController,看起来像这样。
@implementation UIViewControllerCustom
- (id)init
{
self = [super init];
if (self) {
NSLog(@"init");
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton setFrame:CGRectMake(290, 10, 16, 16)];
[infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:infoButton];
}
-(void)showInfo
{
About *about = [[About alloc]init];
NSLog(@"touched");
[about setModalTransitionStyle:UIModalPresentationFullScreen];
[self presentModalViewController:about animated:YES];
}
@end
我的应用中的任何视图控制器都会继承此控制器。触摸信息按钮时,会出现模态窗口。问题是我的一个视图控制器使用UIScrollView。在此控制器信息按钮是可见的,它会在触摸时作出反应。问题是当我触摸按钮时,不会调用UIViewControllerCustom的showInfo方法。我在其他控制器中没有这样的问题。 这可能有什么问题?
答案 0 :(得分:1)
因为UIScrollView处理正在发送的消息。在您的情况下,按钮上的触摸将由UIScrollView处理。
我建议您继承UIScrollView并实现:- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
以确定UIScrollView是否应该处理该事件。
实施可能类似于:
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
if ([view isKindOfClass:[UIButton class]]) {
return NO;
}
return YES;
}