How do I subclass a UILabel object in Interface Builder?

I set the Custom Class: Class attribute of a label to the following class, but the print statement won't fire when I click on the label in Simulator. This worked before. Why does it not work now? Has there been a change in iOS that changed this behavior?


class LabelWithTouches: UILabel {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        
        print("*****")
        
        next?.touchesBegan(touches, with: event)
        
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        
        next?.touchesMoved(touches, with: event)
        
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        
        next?.touchesEnded(touches, with: event)
        
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesCancelled(touches, with: event)
        
        next?.touchesCancelled(touches, with: event)
        
    }

}

Accepted Reply

By default, the property `isUserInteractionEnbled` of `UILabel` is set to false.


With storyboad editor, Show the Attribute inspector (not the Identity Inspector), and check `User Interaction Enabled`.

Replies

Did you enable userInteraction for the UILabel in IB ?

By default, the property `isUserInteractionEnbled` of `UILabel` is set to false.


With storyboad editor, Show the Attribute inspector (not the Identity Inspector), and check `User Interaction Enabled`.