Programmatic Transition from UIBarButtonItem

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

VC example

I create these buttons programmatically, and instead of writing 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. }

}

Then I call Helper.loadNavBarItems(vc: self)from each VC viewDidLoad().

What I'm trying to do now is to call a session when I click one of the buttons (let it be a profile button). So, I need to determine profile.action. So, in the Helper class, I need to write a function segueToProfilethat takes a view ( vc) contoller and runs performSegueWithIdentifier.

, , , Googling, - , , , .

.

.

: , segue .

+4
4

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

UIBarButton, .

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

.

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

barButtonItem:

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

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

:

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

Note. To use the method below, you must set storyBoard Idto 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)
0
source

Source: https://habr.com/ru/post/1662416/


All Articles