How can the action for a custom UIBarButtonItem in Swift be set?
The following code successfully places the button in the navigation bar:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:nil)
self.navigationItem.rightBarButtonItem = b
Now, I would like to call func sayHello() { println("Hello") }
when the button is touched. My efforts so far:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:sayHello:)
// also with `sayHello` `sayHello()`, and `sayHello():`
and..
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(sayHello:))
// also with `sayHello` `sayHello()`, and `sayHello():`
and..
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(self.sayHello:))
// also with `self.sayHello` `self.sayHello()`, and `self.sayHello():`
Note that sayHello()
appears in the intellisense, but does not work.
Thanks for your help.
EDIT: For posterity, the following works:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:"sayHello")
action: "sayHello"
– JiaaroUIBarButtonItem
while the other does not. Requiring beginners to generalize all uses ofselector
can be difficult for them, so I am removing the duplicate status so that people can keep this question up to date. – Suragch