简体   繁体   中英

How to block storyboard segue

I have my two ViewControllers: FormViewController segued to CompleteRegisteringViewController

My FormViewController contains textfields where user writes username, password, confirm password and so on... and a ConfirmButton But it has conditions to complete registering, for example: if the password field text is different to the confirm password field text and so, if it is, the transition between FormViewController and CompleteRegisteringViewController is blocked and the user stay on the FormViewController to modify encountered errors.

I have already my idea to organize code, but the problem is how to "block" the segue ?

You can use shouldPerformSegueWithIdentifier method:

override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
    if identifier == "YOUR SEGUE NAME" {
         // TODO: Do your validation here 
         // Return true if the validation pass, otherwise return false
    }
}

In objective-c You can actually do this by adding following method into your code

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender   {
    if([identifier isEqualToString:@"btnSegue"])
    {
        return YES;
    }
    else
    {
        return NO;
    }

}

Lets assume your segue is btnSegue.And if you need the segue to be performed based on some condition you can have following code

if(check)
{
   [self performSegueWithIdentifier:@"btnSegue" sender:self]; 
}

Where check is BOOL which you can set true or false based on your condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM