Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 views
in Technique[技术] by (71.8m points)

ios - prepareForSegue : how can I set an if-condition?

This may be very rookie but...

I've set a segue between two ViewControllers in my Storyboard, with the identifier clickBtn.

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

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"clickBtn"])
    {
        if(conditionVerified)
        {
            SecondViewController *controller = (SecondViewController *)segue.destinationViewController;
            [controller setManagedObjectContext:self.managedObjectContext];
        }
        else
        {
            // 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To optionally suppress a transition, implement

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender

and return YES or NO, depending on your condition. In your example:

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

In prepareForSegue: it is "too late" to suppress the transition.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...