如何在Swift中设置自定义UIBarButtonItem的操作?
以下代码成功地将按钮放置在导航栏中:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:nil)
self.navigationItem.rightBarButtonItem = b
现在,当按钮被点击时,我想调用func sayHello() { println("Hello") }
。到目前为止我的尝试:
现在,当按钮被点击时,我希望调用func sayHello() { println("Hello") }
。我迄今为止的努力:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:sayHello:)
// also with `sayHello` `sayHello()`, and `sayHello():`
而且...
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(sayHello:))
// also with `sayHello` `sayHello()`, and `sayHello():`
而且...
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(self.sayHello:))
// also with `self.sayHello` `self.sayHello()`, and `self.sayHello():`
请注意,sayHello()
出现在智能感知中,但不起作用。谢谢你的帮助。
编辑:为了后人,以下内容可行:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:"sayHello")
action: "sayHello"
。 - JiaaroUIBarButtonItem
,而另一个问题则没有。要求初学者将selector
的所有用途概括可能对他们来说很困难,因此我取消了重复状态,以便人们可以保持这个问题的最新状态。 - Suragch