2

I have a segue that I mapped from one screen to the next. The first screen always goes to the second page. But I want to make sure that the segue only goes to the second page if some code executed (for example validation checks were ok). Is that possible to set up?

Thanks!

GeekedOut
  • 16,905
  • 37
  • 107
  • 185

3 Answers3

3
-(IBAction)add:(id)sender {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    dispatch_sync(dispatch_get_main_queue(), ^{ 
        [[CMPopupManager sharedObject] showWaitingPopup];    
    });

    sleep(3);

    dispatch_sync(dispatch_get_main_queue(), ^{ 
        [[CMPopupManager sharedObject] closeWaitingPopup];
        [self performSegueWithIdentifier:@"addCarSegue" sender:self];       
    }); 
});
}

(And using Storyboard drag a segue action not from your button but from view controller)

pvllnspk
  • 5,667
  • 12
  • 59
  • 97
1

You could have you segue transition only happen if a certain BOOL is YES. Have a global (as wide as you need, maybe global, maybe just in that scope. depends on your source) BOOL variable, initialized to NO. once validation is OK, change it to YES.

Have the function which calls the next view controller using (don't remember exactly the format) nested inside an if(verificationOK) { }

This way, even that code is reached, it won't get inside the if statement, unless the verificationOK variable has been changed to YES.

EDIT After reading your comments, You could simply trigger the segue programatically. Connect your IBAction to a function, let's say changeView

-(IBAction) changeView:(id) sender {
    if(verificationOk) {
        [self performSegueWithIdentifier:@"SeguesIdentifier" sender:nil];
    }
 }

You can read more about segues here

La bla bla
  • 8,558
  • 13
  • 60
  • 109
  • yeah that is what I was trying to do, but its segueying at the end of the code regardless in which condition it is in. – GeekedOut Aug 08 '12 at 16:48
1

Personally, I'd suggest making sure that that button is disabled until the fields have been changed in such a way that they pass the validation rules. Thus, as those fields change, perform the validation, enabling the segue button once they satisfy the validation rules.

If your validation is complex (e.g. requires some server round trip), then remove the segue from the button, hook your button up to an IBOutlet which validates, and upon success, transitions/segues to the next scene.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • good point about disabling the button until app-side navigation is complete. For the server-side, I do have it so that on button-press, there is a server call, and after the returned data is handled, I try not to do the segue in some cases. But for some reason, the emulator just proceeds to go to the home screen regardless. – GeekedOut Aug 08 '12 at 16:53
  • @GeekedOut Yeah, I don't think you can stop a segue once it's started (so doing something in `prepareForSegue`, or `performSegue` for a custom segue, is too late). I think you'll need to use the `IBOutlet` approach if you need to do that server round-trip in order to validate the data. – Rob Aug 08 '12 at 16:59
  • @Rob can you please explain more about how to accomplish this by IBOutlet? – spacemonkey Aug 01 '14 at 07:49
  • @spacemonkey I said `IBOutlet`, but I really meant `IBAction`. I was thinking of something like the following, http://stackoverflow.com/a/13995767/1271826, where that `IBAction` method does whatever validation necessary, and if and only if it succeeds, it then performs the segue. Alternatively, in iOS 6+, you can use `shouldPerformSegueWithIdentifier:sender:` to validate some input and return `TRUE` if the validation succeeded and the segue should be performed (but that doesn't work well if you need asynchronous call to remote server in order to validate). – Rob Aug 01 '14 at 08:12
  • Oh yes I figured it out already using IBAction and connecting the source view controller to the destination view controller. Thanks though for replying :) – spacemonkey Aug 01 '14 at 08:22