如何設置Swift中的自定義UIBarButtonItem動作?如何在Swift中設置UIBarButtonItem的動作
下面的代碼成功放置按鈕,導航欄:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:nil)
self.navigationItem.rightBarButtonItem = b
現在,我想打電話給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")
您只是把選擇的字符串傳遞選擇在迅速,'行動:「sayHello的」' – Jiaaro
太謝謝你了。我承受壓力要解決這個問題,並且感到沮喪。 – kmiklas
此問題以前被標記爲[@selector()在Swift中的重複?](http://stackoverflow.com/questions/24007650/selector-in-swift)。但是,這個問題具體詢問'UIBarButtonItem',而另一個則沒有。要求初學者概括'selector'的所有用法對他們來說可能是困難的,所以我刪除了重複的狀態,以便人們可以隨時更新這個問題。 – Suragch