我有三個視圖控制器,每個都在導航欄的右側和左側各有兩個按鈕,如下面其中一個所示。以編程方式從UIBarButtonItem
我編程創建這些按鈕,並且代替在每一個相應的視圖控制器(VC)編寫代碼我決定寫創建該按鈕的輔助類。
// Note: I am using FontAwesome as a third-party library.
class Helper: NSObject {
static func loadNavBarItems(vc: UIViewController) {
let profileButton = UIBarButtonItem()
let addButton = UIBarButtonItem()
let attributes = [NSFontAttributeName: UIFont.fontAwesome(ofSize: 20)] as [String: Any]
profileButton.setTitleTextAttributes(attributes, for: .normal)
addButton.setTitleTextAttributes(attributes, for: .normal)
profileButton.title = String.fontAwesomeIcon(name: .userCircle)
addButton.title = String.fontAwesomeIcon(name: .plus)
vc.navigationItem.leftBarButtonItem = profileButton
vc.navigationItem.rightBarButtonItem = addButton
}
func segueToProfile(vc: UIViewController) { // I need help here. }
}
我然後調用每個VC的viewDidLoad()
Helper.loadNavBarItems(vc: self)
。
我現在要做的是在按下其中一個按鈕時觸發一個segue(讓我們假設它是配置文件按鈕)。所以,我需要定義profile.action
。所以,在助手類中,我必須編寫一個函數segueToProfile
,它需要一個視圖控制器(vc
)並運行performSegueWithIdentifier
。
問題是,我不完全理解如何通過選擇器傳遞不同類型的參數,並且我可能在Google上很糟糕,但我找不到任何接近我的地方的問題,以瞭解如何做到這一點。
非常感謝您的幫助。
僅供參考this is the structure of my Storyboard。
編輯:如故事板結構屏幕截圖所示,我已經從三個視圖控制器中的每一個創建了一個segue到目標視圖控制器。
通過選擇器,你願意發送什麼樣的參數?同樣在你的故事板模擬中,你正在展示導航控制器。所以你應該明白destinationController會是NavigationController,而不是ViewCotnroller。另一件事是,你可能應該爲UIViewController創建一個擴展,並將所有創建和分配BarButton的代碼都替換爲它。這樣,你避免傳遞UIViewController參數到'loadNavBarItems' func –
@noir_eagle謝謝,但我的問題是與理解UIBarButtonItem操作和選擇器。我試圖觸發一個segue當其中一個UIBarButtonItems被挖掘,我試圖弄清楚如何在使用'profileButton.action'時傳遞參數給選擇器 –
這個導航控制器在每個頁面上的這些按鈕?或者只有一個ViewController? –