touchesBegan not called in UIView subclass
Asked Answered
K

2

9

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.

Kenn answered 25/5, 2015 at 17:20 Comment(6)
What do you mean by "not working"?Hurrah
I mean that when I touch the view, No AlertView show upKenn
tried using a simple println() in the function to see if it's not a bug with the alertview?Portie
Actually, I used a breakpoint in the function but the app never stoppedKenn
Is userInteractionEnabled enabled on your view's superviews?Farmer
Damn, that was so simple. Thank you so much 0x7fffffff♦ !Kenn
F
14

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.

Farmer answered 25/5, 2015 at 17:29 Comment(1)
Not getting desired result, though I have view->scrollview->view layout,and in both views, I have set userInteractionEnabled to trueGotha
J
3

Maybe some of the superviews has the userInteractionEnabled set to "false"?

Judiciary answered 25/5, 2015 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.