Fares K. A.
Fares K. A.

Reputation: 1285

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.

Upvotes: 4

Views: 2929

Answers (4)

Joe
Joe

Reputation: 8986

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)

Upvotes: 1

Paweł
Paweł

Reputation: 1271

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")
 }

Upvotes: 0

unniverzal
unniverzal

Reputation: 803

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.

Upvotes: 0

Alessio Campanelli
Alessio Campanelli

Reputation: 1020

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

Upvotes: 0

Related Questions