UIButton not calling action in iOS 5 but works in iOS 6

╄→гoц情女王★ 提交于 2019-12-03 02:36:56

You have explained very beautifully the cause of the problem. On iOS 5, a UITapGestureRecognizer on a button's superview interferes with the action of the button. On iOS 6, they fixed this: they introduced a UIView event gestureRecognizerShouldBegin:, and a button automatically returns NO for a tap gesture recognizer attached to a superview.

For iOS 5, you'll need to use a delegate method on the tap gesture recognizer to stop it from recognizing if the tapped view was the button.

In my case, I was also using a general UITapGestureRecognizer for dismissing the keyboard opened by a text field when the user would tap anywhere on the main view.

I fixed this by only adding a gesture recognizer when the keyboard shows up (see

- (void)keyboardWillShow:(NSNotification *)n or

- (BOOL)textFieldShouldBeginEditing:(VDTextFieldWithError *)textField)

and then removing the gesture recognizer when hiding the keyboard (thus removing the selector in the selector method of the recognizer itself)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!