Reputation: 4032
I have created several scenes within a storyboard file in my Xcode project, and the first scene that is loaded is a user login in screen. When a user logs in the UIButton "Login" takes the user to the welcome screen. I setup that in storyboard using a modal segue. I want a user with the name "admin" to be taken a welcome admin screen. I'm pretty sure it's only possible to have one segue associated with one object, i.e. UIButton in storyboard, so I am stumped as to how I can accomplish the login to take the admin to the admin welcome screen, and take all other users to the user welcome screen. I really don't want to create two separate login buttons, so that's not an option.
I came across some stackoverflow posts with similar questions but all the answers seemed a little convoluted. Keep in mind I am new to Xcode, so if you paste code in your answer please specify which file the code should go in (that would help me out A LOT). I'll post a picture of what my storyboard looks like so far to help demonstrate a visual diagram of what I am talking about.
Upvotes: 5
Views: 2671
Reputation: 3727
If I understood your question correctly, here are the steps you need to take to make it work:
From the WelcomeViewController
– where you have the username / password login button, control+drag from the UIViewController
to the WelcomeScreen
, and choose Modal
from the pop up menu (By drag from UIViewController I mean, select the UIViewController
in storyboard, there is a round shape icon on the bottom, from that to the destination view controller). Name the segue identifier as "UserSegue"
Repeat step 1, but instead control+drag it to Admin
screen. Name the segue identifier as "AdminSegue"
You need now to subclass WelcomeViewController
,
Implement -(IBAction)login:(id)sender
- (IBAction)login:(id)sender { // assuming you have hooked up the user name text field if ([self.usernameTextField.text isEqualToString:@"admin"]) { [self performSegueWithIdentifier:@"AdminSegue" sender:sender]; } else { [self performSegueWithIdentifier:@"UserSegue" sender:sender]; } }
In storyboard, hook up the login button to the method in step 4.
Upvotes: 6
Reputation: 126107
That's right -- you can only have one outgoing segue from a button (or other control).
However, you can have any number of outgoing segues from a view controller. Unlike those attached to a button (or other control), segues from a view controller aren't invoked automatically when a control is tapped -- you have to do it in code, but doing it in code gives you the flexibility to do it according to whatever conditions you need.
To make a segue from a view controller, control-drag from the view controller icon below its view (the orange circle) in IB, or from its entry in the scene outline. After creating the segue, give it a unique identifier (e.g. "mySegue").
Then, create an action method hooked up to your button. In that method's implementation (which presumably is in the view controller containing the button), test whichever conditions you need, and call [self performSegueWithIdentifier:@"mySegue"]
to perform the segue.
There's more explanation in my answer to a similar question, if that helps.
Upvotes: 1