user1253637
user1253637

Reputation: 237

How to cancel a segue programmatically and remain in the same view

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.

Upvotes: 3

Views: 3905

Answers (3)

Bogdan Ustyak
Bogdan Ustyak

Reputation: 5839

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

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

Upvotes: 0

shim
shim

Reputation: 10116

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

Upvotes: 2

JoshRagem
JoshRagem

Reputation: 595

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.

Upvotes: 0

Related Questions