我爲UINavigationItem(rightBarButtonItem)實現了一個非常標準的選擇按鈕。我正在做的方式是在viewDidLoad函數中實例化它。我將一個選擇器作爲一個動作傳遞,然後在viewDidLoad函數之外寫入該選擇器函數。在它的選擇器中引用UIBarButtonItem動作函數
明顯的問題是,導航選擇按鈕不再處於選擇器功能的範圍之內,所以我無法引用和使用它。
必須有許多方法來實現選擇導航按鈕。我希望它在集合視圖中充當開關,因此只有在選擇按鈕被擊中後,才能選擇並高亮顯示集合視圖單元格。爲此,我的選擇器動作將打開和關閉myButton.selected布爾值,就像一個開關(所以我不認爲我需要一個UISwitch)。
有足夠的解釋,這裏是代碼;
class GalleryViewController: UIViewController {
@IBOutlet var collectionView: UICollectionView!
....
override func viewDidLoad() {
super.viewDidLoad()
...
//MARK: Select button
let mySelector: Selector = #selector(GalleryViewController.selectSelector)
let selectButton: UIBarButtonItem = UIBarButtonItem(title: "Select", style: UIBarButtonItemStyle.Plain, target: self, action: mySelector)
self.navigationItem.rightBarButtonItem = selectButton
}
//MARK: Select Button Function
func selectSelector() {
//Toggle selectButton ON/OFF here, and more
}
如果你創建的導航欄barButtonItem切換collectionViewCells狀態的一個極好的方法,我很樂意聽到它!只是提供一個完整的答案,不要以爲我知道很多(仍然覺得我在Xcode中面對面)
完美的使用發送者/接收者,謝謝! – Typhon