Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

ios - Programmatically segue from UIBarButtonItem

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To create barButtonItem:

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

To create action and segue for barButtonItem:

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

Note : To perform segue you have to give segue identifier name from your storyboard.

enter image description here

Output:

enter image description here

Updated:

If you want to connect your destVC without segue you can use below method:

Note: To use below method you have to set storyBoard Id in identity inspector.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...