20

In my app I have a login screen. When connecting a segue from the login button to the next (table) view controller however, you are always able to proceed, no matter what you type in as login information. How can I check the login information and then decide whether to perform the segue or not, so how to perform the segue conditionally?

Is it possible to achieve this while working with a segue? I know it can be done programmatically, but then I need another navigation controller cause I need a navigation bar..

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
mmvie
  • 2,571
  • 7
  • 24
  • 39

4 Answers4

20

You can wire up your login button or whatever to some IBAction code, decide if the login should proceed and then (if it should) you can use performSegueWithIdentifier: to transition to the new view.

I just wrote another post about using this method, here.

Community
  • 1
  • 1
Simon
  • 8,981
  • 2
  • 26
  • 32
  • Incidentally, you can also have your navigation controller without the navigation bar. – Simon Oct 23 '11 at 14:48
  • 1
    This does not stop your segue from being performed if the login data is invalid.. – mmvie Oct 23 '11 at 14:55
  • 2
    No, you'd check that the login was valid before you perform the action. If it's not valid, you don't use performSegueWithIdentifier. If you downloaded the example - you'd see in the code where you can perform the check before pushing the next view. – Simon Oct 23 '11 at 15:25
  • Using performSegueWithIdentifier implies that the segue exists.. By creating this segue (drag it from button to view controller in IB) it doesn't matter whether you put performSegueWithIdentifier in your code or not.. Once the button gets clicked, segue is performed.. Your code has nothing to do with my problem. I really need to hold the segue with a block of code or something.. – mmvie Oct 23 '11 at 15:36
  • 7
    You drag the segue from the View Controller to the new view, not from the button to the new view, hence the ability to connect more than one button without Xcode warnings about multiple use of Segue. Try connecting your segue from the status bar in view 1, to the second view and then coding the rest. Again, the example I provided shows this wiring without it being hooked to a button. – Simon Oct 23 '11 at 15:53
  • Oh, it's in the details again. Thanks for you fast help ! :) – mmvie Oct 23 '11 at 15:54
10

On iOS 6, UIViewController's shouldPerformSegueWithIdentifier:sender: method solves this issue without manual triggering segues.

Nikolay Mamaev
  • 1,474
  • 1
  • 12
  • 21
3
  1. Create a manual segue from "login" to say "dashboard"
  2. Give a name for segue identifier, e.g. "showDashboard"
  3. Wire the "Next" button for IBAction
  4. Manually invoke segue with identifier once all the constraints are met.

- (IBAction)nextButtonPressed:(id)sender
{
    if ([self hasAllRequiredFields]) {
        [self performSegueWithIdentifier:@"showDashboard" sender:self];
    } 
}

Ramesh
  • 1,703
  • 18
  • 13
2

This is belated, but imo it's best to setEnabled:NO whatever is triggering the segue until the segue is "valid". That separates your segue logic from your view logic....

Kaolin Fire
  • 2,521
  • 28
  • 43