Question

I want to know how do we cancel a segue in program when some condition is false?

Below is my code snippet:

if ([segue.identifier isEqualToString:@"Information Segue"])
{   
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Login" inManagedObjectContext:self.managedObjectContext];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@" ANY userid==%@ AND password==%@",_loginTextfield.text, _passwordTextField.text];
    [request setEntity:entity];
    [request setPredicate:predicate];
    NSError *anyError=Nil;
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:request error:&anyError];
    if ([fetchedObjects count] )
    {
        useridentered = _loginTextfield.text;
        passwordentered = _passwordTextField.text;

        InformationTVC *informationTVC = segue.destinationViewController;
        informationTVC.managedObjectContext = self.managedObjectContext;             
    }
    else
    {
       /*  want to put my cancel the segue and remain in the login view */ 
    }
}

If any one could guide me it would be a great help.

Was it helpful?

Solution

The reference for the segue: https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIStoryboardSegue_Class/Reference/Reference.html Has no public method that would allow you to cancel the segue.

It seems to me that would be easier to decide to do the segue or not, based on the conditions, before the segue starts happening, but that is up to you.

OTHER TIPS

Check out this thread: Conditional segue performed on tap on UITableViewCell

Basically instead of linking your segue directly to a button, put it between the view controllers, and link the button to an IBAction which will conditionally call the function performSegueWithIdentifier

There is another option here: https://stackoverflow.com/a/42161944/4791032

You can just check it in func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top