Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action not working UIBarButtonItem

func showFilterPanel() {
    println("Showing")
}

let backButton = UIBarButtonItem(title: "Horse", style: UIBarButtonItemStyle.Plain, target: nil, action: "showFilterPanel")

@IBAction func tapGesture(sender: AnyObject) {
    self.navigationItem.leftBarButtonItem = self.backButton
}

The above code is embedded in a Map Navigation Controller. The action has no effect, any ideas why (I don't see "Showing" in the console)?

like image 847
jskidd3 Avatar asked Jul 23 '26 21:07

jskidd3


1 Answers

You passed nil as the target for the selector. It should be self in this case:

let backButton = UIBarButtonItem(title: "Horse", style: UIBarButtonItemStyle.Plain, target: nil, action: "showFilterPanel")

You'll also need to rearrange this to move the instantiation of the UIBarButtonItem down to method scope in order to be able to reference self in its instantiation. When you create a property in Swift you can't reference other properties or self in its creation, unless it's a computed or lazily instantiated property.

like image 184
Mick MacCallum Avatar answered Jul 26 '26 14:07

Mick MacCallum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!