Search code examples
iosobjective-ccocoa-touchswiftuiview

Programmatically created custom view with button target


I'm having an custom UIView subclass which has some UIButtons appended. For every button there is an target action defined on the UIViewController which is including the custom UIView.

Somehow the TouchUpInside event is never being caught within the custom view including ViewController. Since UIViews are part of the UIResponderChain I wonder why events are not being fired? Does it make any sense to delegate the target action or should this be done another way?

Custom UIView Subclass: CustomView

let button = UIButton(frame: CGRectMake(10.0, 10.0, 100.0, 100.0))
button.addTarget(self.delegate, action: "buttonTapped:", forControlEvents: .TouchUpInside) 

ViewController

// Include the custom view
let customView = CustomView()
customView.delegate = self
self.view.addSubview(customView)
...

func buttonTapped(sender: AnyObject!) {
    // Doesn't get called
}

Solution

  • Unfortunately I was missing to declare the frame rect for the buttons - so buttons are being displayed, but they're indeed not clickable.