当我添加 viewDidLoad 时 IBAction 不会触发
Posted
技术标签:
【中文标题】当我添加 viewDidLoad 时 IBAction 不会触发【英文标题】:IBAction does not fire when I add viewDidLoad 【发布时间】:2011-05-23 04:04:00 【问题描述】:我有几个 IBAction 附加到 UIButtons。在我添加以下代码之前,IBActions 工作正常:
- (void)viewDidLoad
[super viewDidLoad];
NSLog(@"View Did Load");
[self addGestureRecognizersToView:drawImage];
在我添加那段代码后,IBActions 不会触发。当我触摸 UIButtons 时它们会突出显示,但没有任何 IBAction 代码被命中。
这是我的 addGestureRecognizers 代码:
- (void)addGestureRecognizersToView:(UIImageView *)theView
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[panGesture setMaximumNumberOfTouches:2];
[panGesture setMinimumNumberOfTouches:1];
//panGesture.delegate = drawImage;
[theView addGestureRecognizer:panGesture];
[panGesture release];
UITapGestureRecognizer *doubleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleFingerTap setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:doubleFingerTap];
[doubleFingerTap release];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singleFingerTap setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:singleFingerTap];
[singleFingerTap release];
如果我注释掉 singleFingerTap 代码,它就可以工作。我猜我不应该使用 alloc,因为我之前已经在 doubleFingerTap 中分配过一次?
关于我可能在这里遗漏的任何想法?
【问题讨论】:
addGestureRecognizersToView:
是做什么的?
您的代码 sn-p 中没有任何内容会干扰您的 IBAction。你能发布 addGestureRecognizersToView 吗?
是的,有一些东西 //[self addGestureRecognizersToView:drawImage];当我评论那一行时,按钮又开始工作了。
【参考方案1】:
您的单指点击阻碍了按钮的正常行为。您必须确保触摸畅通无阻。
[singleFingerTap setCancelsTouchesInView:NO];
【讨论】:
【参考方案2】:听起来 UITapGestureRecognizer 是 intercepting the taps,否则它会由 UIButtons 处理。
你可以使用gestureRecognizer:shouldReceiveTouch:
:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
return (touch.view != self.button1 &&
touch.view != self.button2);
或者,您可以hack the responder chain。
【讨论】:
以上是关于当我添加 viewDidLoad 时 IBAction 不会触发的主要内容,如果未能解决你的问题,请参考以下文章