Programmatically segue from UIBarButtonItem

2019-01-28 06:49发布

I have three view controllers, all with two buttons each on the right and left sides of the navigation bar, as seen below on one of them.

Example VC

I'm creating these buttons programatically, and instead of writing the code in each respective view controller (VC) I decided to write a Helper class that creates the buttons.

// 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. }

}

I then call Helper.loadNavBarItems(vc: self) from each VC's viewDidLoad().

What I'm trying to do now is to trigger a segue when one of the buttons is pressed (let's assume it's the profile button). So, I need to define profile.action. So, in the Helper class, I have to write a function segueToProfile that takes a view contoller (vc) and runs performSegueWithIdentifier.

The problem is, I'm not fully understanding how to pass in different types of parameters through selectors, and I may be bad at Googling but I cannot find any questions that are close enough to mine for me to understand how to achieve this.

Many thanks in advance for your help.

For reference, this is the structure of my Storyboard.

EDIT: As shown in the storyboard structure screenshot, I've already created a segue from each of the three view controllers to the destination view controller.

4条回答
手持菜刀,她持情操
2楼-- · 2019-01-28 07:11

To create barButtonItem:

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "                                                                    
查看更多
\"骚年 ilove
3楼-- · 2019-01-28 07:18

You can create barButtonItems with selectors :

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

Where the didPressLeftButton is a function :

 func didPressLeftButton(){
        print("Did press left button")
 }
查看更多
Deceive 欺骗
4楼-- · 2019-01-28 07:24

i don't know if i understand your problem, but for passing data between viewControllers(embedded in a UINavigationController in your case) using segue, you can do it in:

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
}}
查看更多
迷人小祖宗
5楼-- · 2019-01-28 07:34

To add an action to a UIBarButton you should use one of its initializers. For example

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

Instead of title , you can also set an Image to that button.

查看更多
登录 后发表回答