touchesBegan not called in UIView subclass
13,466
Solution 1
You need to make sure that userInteractionEnabled
is enabled on all of the superviews of your view. If any of the superviews have this property set to false
, the touch events won't be processed for them or any of their subviews.
Solution 2
Maybe some of the superviews has the userInteractionEnabled set to "false"?
Author by
La masse
Been working on C++, C, Java, Java for android, objective-c, Swift Also on webdesign with Javascript, HTML, CSS And data management with PHP and SQL
Updated on July 24, 2022Comments
-
La masse almost 2 years
I know there already are some questions about this but I couldn't find a solution. There is a very simple code that works just fine on one of my projects but not on an another one:
Here is the code for subclass of UIView.
import UIKit class test : UIView { init(frame: CGRect, color: UIColor) { super.init(frame: frame) self.backgroundColor = color self.userInteractionEnabled = true } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { super.touchesBegan(touches, withEvent: event) var alert = UIAlertView() alert.title = "lol" alert.message = "lol" alert.addButtonWithTitle("Ok") alert.show() } }
I'm sure the solution is quite simple but I can't find it.
-
rdelmar almost 9 yearsWhat do you mean by "not working"?
-
La masse almost 9 yearsI mean that when I touch the view, No AlertView show up
-
Wraithseeker almost 9 yearstried using a simple println() in the function to see if it's not a bug with the alertview?
-
La masse almost 9 yearsActually, I used a breakpoint in the function but the app never stopped
-
Mick MacCallum almost 9 yearsIs
userInteractionEnabled
enabled on your view's superviews? -
La masse almost 9 yearsDamn, that was so simple. Thank you so much 0x7fffffff♦ !
-
-
Sachin Sharma over 6 yearsNot getting desired result, though I have view->scrollview->view layout,and in both views, I have set userInteractionEnabled to true