-1

I programmatically created a toolbar that has 2 bar button items: btnPlus and btnRating (fixedSpace is just used to set the distance between them). What I want to do is when a user clicks on btnPlus, the button will direct user to the FifthViewController and when the user taps on btnRating it will direct user to the SixthViewController. I have the below code but how can I know which button the user has clicked in this case and based on that direct it to the relevant viewController? I'd appreciate any help! Thank you very much in advance!

navigationItem.rightBarButtonItems = [btnPlus, fixedSpace, btnRating]

@objc func OnbtnPlusTouched()
{
    guard let uivc = storyboard!.instantiateViewController(withIdentifier: "FifthViewController") as? FifthViewController else {
        return
    }
    navigationController!.pushViewController(uivc, animated: true)
David Berry
  • 40,941
  • 12
  • 84
  • 95
Jacky
  • 3
  • 2
  • https://stackoverflow.com/questions/39768600/how-to-programmatically-set-action-for-barbuttonitem-in-swift-3/44582131 – sazid008 Aug 27 '18 at 09:58

1 Answers1

0

You can add methods to barButton like this :

let btnPlus  = UIBarButtonItem(title: "Your Title", style: .plain, target: self, action: #selector(YourViewController.OnbtnPlusTouched))

If your using image in that then :

let btnPlus = UIBarButtonItem(image: "Image Name", style: .plain, target: self, action: #selector(YourViewController.OnbtnPlusTouched))

similarly you can do that for btnRating.

One more thing, your methods name is OnbtnPlusTouched, which is not good. Create methods with lowerCamelCase as onBtnPlusTouched.

To know more you can go through this helpful URL:

Hope this helps.

Amit
  • 4,837
  • 5
  • 31
  • 46