Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create two segues for a single button in a view controller?

Tags:

ios

swift

iphone

I have a root view controller - which is a simple page presented to the user. In this page, there is a UIBarButton that goes to the user profile page.

When the user is in its profile, he/she'll have a back button that will allow him/her to go back to the root view controller.

However, the root view controller is different for admins and clients (there is one standard view controller for admins and one standard view controller for clients).

How can I create two segues that are triggered by the 'Back' profile button? I mean, depending on who is currently logged in (an admin or a client), I would like to go back to their respective pages.

The client cannot go back to the admin's page and neither vice versa.

Is there any way to create these two segues? Or even another/better way? I have tried to do that using storyboard, but when I create the second segue, it deletes the first one linked to the button.

like image 702
Banjo Avatar asked Sep 12 '25 07:09

Banjo


2 Answers

You cannot do that but there is other way for that, create two segue bwtween SenderViewController and DestinatrionViewController. After that set action for your button and perform the segue according to your requirements.

@IBAction func buttonAction(_ sender: UIButton) {
    if condition {
        self.performSegue(withIdentifier: "identifier1", sender: nil)
    }
    else {
        self.performSegue(withIdentifier: "identifier2", sender: nil)
    }
}
like image 53
Nirav D Avatar answered Sep 14 '25 21:09

Nirav D


Going off of Nirav D's answer, use an if...else with the condition that they are an admin or client. But don't control+drag from the button itself, control+drag from the ViewController as seen in the images below. Click and drag to admin VC Click and drag to client VC

like image 20
nviens Avatar answered Sep 14 '25 19:09

nviens