Vlad Alexeev
Vlad Alexeev

Reputation: 2224

IBAction for a standard button doesn't work

It's basic and should work out of the box, so I don't have any clue why it doesn't . I tried to remove link to IBAction and rewritten it again - the Log doesn't show that it was clicked. Weird.

enter image description here

here's the code :

@IBAction func onManClicked(sender: AnyObject) {
    NSLog("onManClicked")
    person.gender = 1
    genderUpdate()

}

The NSLog part never triggered. So, none of the four buttons I have in this ViewController respond. Where the problem could be?

This is how I add this whole view - I think this is the problem.

    let vc = PersonalDialog(nibName :"PersonalDialog", bundle: nil, section: section, position: position, person: Person())
    vc.view.frame = UIScreen.mainScreen().bounds
    parentVC.view.addSubview(vc.view)

I'm new to iOS developement, I don't know hot to do it right - I understand now that I added a subview but didn't add the controller for it. Probably.However, it does call the viewDidAppear method

Upvotes: 1

Views: 2473

Answers (2)

Have you tried checking if button interaction is enabled nor its parent view? Had it been overlapped by some object or other views?

I experienced this before and I just checked all this and it somewhat fixed the problem.

Upvotes: 1

pbodsk
pbodsk

Reputation: 6876

You say:

I tried to remove link to IBAction and rewritten it again

So... Try deleting the connection in Interface Builder and then reconnect (you've probably already done that)

Another thing to look out for.

If I, in Swift 3, create a method like this:

    @IBAction func buttonClicked(sender: AnyObject) {
        print("hello")
    }

and then try to connect to that then the connection looks like this

enter image description here

Notice the "with sender" part, which I didn't add but which is a part of the new Swift 3 parameter naming.

To avoid that, I have to rename my method to this:

@IBAction func buttonClicked(_ sender: AnyObject) {
    print("hello")
}

(notice the _ before sender)

And if I then reconnect:

enter image description here

things look as expected. Maybe that is what has bitten you too? Interface builder looking for an @IBAction called onManClickedWithSender and you defining an @IBAction called onManClicked.

Hope this helps you.

Upvotes: 0

Related Questions