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")
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…