Rob Rob - 1 year ago 67
iOS Question

prepareForSegue : how can I set an if-condition?

This may be very rookie but...

I've set a segue between two

in my
, with the identifier

Now I'm calling it this way in my code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([[segue identifier] isEqualToString:@"clickBtn"])
SecondViewController *controller = (SecondViewController *)segue.destinationViewController;
[controller setManagedObjectContext:self.managedObjectContext];
// If I enter here I get the error below

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Link''

In fact I don't want to do this transition if I enter the else-statement. How to do so?

Answer Source

Rather than having the button perform the segue have it call a method like the following.

      [self performSegueWithIdentifier:@"clickBtn" sender:sender];
        // do not perform segue and do other relevant actions.

The error you are seeing is because i am assuming controller is calling something that needs context manager not to be nil.

You can't really stop the segue from occurring once you are in the method prepareForSegue as it will always perform the segue at the end of the method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download