UIButton Subclass Ignoring 'Touch Up Inside' Event

I have a subclass of a UIButton:


import UIKit 
class CustomButton: UIButton { 
    required init?(coder decoder: NSCoder) { 
      super.init(coder: decoder)

      //Customize Button Appearence Here..

    } 
}


In interface builder, in storyboards, under my main view controller, I dragged and dropped a stock button, and changed it's 'Custom Class' to CustomButton from the drop down menu.


Wired it's 'Touch Up Inside' event to an IBAction.


Custom code for appearance shows up, but the button is not firing the 'Touch Up Inside'.


When I checked for touch events (touch began, ended, eat) within the subclass, the button seems to be receiving touch.

Answered by OOPer in 125627022

I copied your code and put a CustomButton on the storyboard, and connected its Touch Up Inside event to the ViewController's customButtonPressed(_:) method, I'm sure the action method was called.


One possibility is that you have mis-connected the 'Touch Up Inside' event. Try re-connecting it or call addTarget(_:action:forControlEvents:) manually.


Other possibilities ..., cannot guess without more info.

Accepted Answer

I copied your code and put a CustomButton on the storyboard, and connected its Touch Up Inside event to the ViewController's customButtonPressed(_:) method, I'm sure the action method was called.


One possibility is that you have mis-connected the 'Touch Up Inside' event. Try re-connecting it or call addTarget(_:action:forControlEvents:) manually.


Other possibilities ..., cannot guess without more info.

I found out this after reading your reply:


When I customize the button, I added a UIVisualEffectView as its subview that covered the entire view, and that canceled the any events the button was able to take. Quite unfortunate.


I had to go with a UIView subclass and add a tap gesture to mimic the touch events.

UIButton Subclass Ignoring 'Touch Up Inside' Event
 
 
Q