Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One UIButton two scenes only one segue, conditional code needed

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.

enter image description here

like image 319
ipatch Avatar asked Jun 09 '12 02:06

ipatch


1 Answers

If I understood your question correctly, here are the steps you need to take to make it work:

  1. 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"

  2. Repeat step 1, but instead control+drag it to Admin screen. Name the segue identifier as "AdminSegue"

  3. You need now to subclass WelcomeViewController,

  4. 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];
        }
    }
    
  5. In storyboard, hook up the login button to the method in step 4.

like image 50
Canopus Avatar answered Oct 12 '22 23:10

Canopus