UIButton in Swift is not registering touches

ios, swift, uibutton

Solution

I see it is an old question but I just faced very similar problem yesterday. My buttons were highlighted on touch but not firing the action.

I have two view controllers. One view covers the others view and button is on the top view.

eg.

Rootviewcontroller has back view

Topviewcontroller has top view

The button on top view does not call the action if I don't add the topviewcontroler as childviewcontroller of the rootviewcontroller. After adding the topviewcontroller as childviewcontroller it started to working.

So in short: just try to add the view controller of buttons superview as childviewcontroller to the parent views viewcontroller with the following method

func addChildViewController(_ childController: UIViewController)

Problem

I'm trying to create a UIButton using Swift. It compiles fine and I can see my button in the simulator, but when I click it, nothing happens. This is the code I am using: ``` let settings = UIButton() settings.addTarget(self, action: "touchedSet:", forControlEvents: UIControlEvents.TouchUpInside) settings.setTitle("Settings", forState: .Normal) settings.frame = CGRectMake(0, 530, 150, 50) scrollView.addSubview(settings) ``` In the same class, here is the function 'touchedSet': ``` func touchedSet(sender: UIButton!) { println("You tapped the button") } ``` I'm using the simulator as I don't have an iOS 8.0 device, could that be the problem? Thanks!

Original source

Related problems