2016-11-30 103 views
4

我有三個視圖控制器,每個都在導航欄的右側和左側各有兩個按鈕,如下面其中一個所示。以編程方式從UIBarButtonItem

Example VC

我編程創建這些按鈕,並且代替在每一個相應的視圖控制器(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到目標視圖控制器。

+0

通過選擇器,你願意發送什麼樣的參數?同樣在你的故事板模擬中,你正在展示導航控制器。所以你應該明白destinationController會是NavigationController,而不是ViewCotnroller。另一件事是,你可能應該爲UIViewController創建一個擴展,並將所有創建和分配BarButton的代碼都替換爲它。這樣,你避免傳遞UIViewController參數到'loadNavBarItems' func –

+0

@noir_eagle謝謝,但我的問題是與理解UIBarButtonItem操作和選擇器。我試圖觸發一個segue當其中一個UIBarButtonItems被挖掘,我試圖弄清楚如何在使用'profileButton.action'時傳遞參數給選擇器 –

+0

這個導航控制器在每個頁面上的這些按鈕?或者只有一個ViewController? –

回答

0

我不知道如果我理解你的問題,但對於viewControllers之間傳遞數據(嵌入UINavigationController你的情況)使用SEGUE,你可以做到這一點的:

override func prepare(for segue: UIStoryboardSegue, sender: Any?){ 
    if(segue.identifier == "yourIdentifier"){ 
     let navPreview : UINavigationController = segue.destination as! UINavigationController 
     let preview : YourViewController = navPreview.viewControllers[0] as! YourViewController 
}} 
+0

謝謝Alessio,但我的問題是與UIBarButtonItem操作和選擇器。我試圖在觸發一個UIBarButtonItems時觸發一個segue。 –

+0

好的,但你有問題觸發一個新的控制器的賽格?因爲在這種情況下,從故事板,你可以使用控制+拖動到你的控制器和檢查(關係segue(視圖控制器))和自動Xcode創建一個從tabbarController到UIViewController的segue –

0

要添加行爲到UIBarButton你應該使用它的一個初始值設定項。 例如

let profileButton = UIBarButtonItem(title: "Your title", style: .done, target: self, action: #selector("Call your function to push View controller")) 

取而代之的標題,你也可以將圖像設置到該按鈕。

+0

謝謝,但你能告訴我如何添加一個參數採取的行動? –

0

您可以選擇創建barButtonItems:

let leftBarButton = UIBarButtonItem(image: image, landscapeImagePhone: image, style: UIBarButtonItemStyle.bordered, target: self, action: #selector(didPressLeftButton)) 

凡didPressLeftButton是一個函數:

func didPressLeftButton(){ 
     print("Did press left button") 
} 
+0

謝謝,但你能告訴我如何添加一個參數的操作? –

+0

看看這裏的教程,他們有幾個很好的例子與參數:https://www.bignerdranch.com/blog/hannibal-selector/ –

+0

只是另一個想法@ FaresK.A。 - 你真的需要傳遞參數嗎?如果每個視圖都有自己的導航按鈕,可以將功能didPressLeft,didPressRight附加到視圖控制器。爲了使其更具可重用性,您可以使用基本視圖控制器對其進行子類化,該控制器將處理創建按鈕。 –

0

要創建barButtonItem:

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(ProfileButtonTapped)) 

要創建行動和Segue公司的barButtonItem :

func ProfileButtonTapped() { 
    print("Button Tapped") 
    performSegue(withIdentifier: "YourSegueIdentifierName", sender: self) 
    //If you want pass data while segue you can use prepare segue method 
    } 

注:perform segue你必須從你的故事板給segue identifier name

enter image description here

輸出:

enter image description here

更新時間:

如果你想你的destVC連接而無需賽格瑞您可以使用下面的方法:

ñ注意:要使用以下方法,您必須在identity inspector中設置storyBoard Id

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let DestVC = storyboard.instantiateViewController(withIdentifier: "DestVcName") as! DestVcName //UINavigationController 
self.present(DestVC, animated: true, completion: nil) 
相關問題