3

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.

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
user1253637
  • 237
  • 1
  • 7
  • 15
  • Why dont you not use the IB and instead move completely programatically in this case? you can easily on the press of a button or whatever u want, display the new viewcontroller or not. – Pochi Mar 12 '12 at 03:11
  • Because i have used table view controller in my whole project and don't want to disturb it at the last moment... – user1253637 Mar 12 '12 at 03:13
  • In this case, you really need to call this segue from code instead of automatically from the storyboard. i.e. if you press a button to transition to this view, set the action to a routine in your view controller and check to see if you should show the view controller in the first place. If so, then either push it or call the segue from the storyboard, otherwise display the error message or whatever you need to do. You don't have to change how you do the rest of the transitions though! – lnafziger Mar 12 '12 at 16:13
  • @Inafziger.. I did the same.... and it worked... thanks anyways :) – user1253637 Mar 13 '12 at 01:53

3 Answers3

2

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

Community
  • 1
  • 1
shim
  • 9,289
  • 12
  • 69
  • 108
0

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

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

Community
  • 1
  • 1
Bogdan Ustyak
  • 5,639
  • 2
  • 21
  • 16
0

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.

JoshRagem
  • 575
  • 3
  • 10