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"?

Share:
13,466
La masse
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, 2022

Comments

  • La masse
    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
      rdelmar almost 9 years
      What do you mean by "not working"?
    • La masse
      La masse almost 9 years
      I mean that when I touch the view, No AlertView show up
    • Wraithseeker
      Wraithseeker almost 9 years
      tried using a simple println() in the function to see if it's not a bug with the alertview?
    • La masse
      La masse almost 9 years
      Actually, I used a breakpoint in the function but the app never stopped
    • Mick MacCallum
      Mick MacCallum almost 9 years
      Is userInteractionEnabled enabled on your view's superviews?
    • La masse
      La masse almost 9 years
      Damn, that was so simple. Thank you so much 0x7fffffff♦ !
  • Sachin Sharma
    Sachin Sharma over 6 years
    Not getting desired result, though I have view->scrollview->view layout,and in both views, I have set userInteractionEnabled to true