prepareForSegue : how can I set an if-condition?

余生长醉 提交于 2019-11-30 09:19:03

问题


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?


回答1:


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

-(IBAction)mySegueButtonMethod:(id)sender
{
   if(conditionVerified){
      [self performSegueWithIdentifier:@"clickBtn" sender:sender];
   }
   else{
        // 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.




回答2:


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.




回答3:


You should refactor your code differently for this purpose. Implement shouldPerformSegueWithIdentifier like below:

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

It would prevent the segue "clickBtn" to be executed if the condition is not verified, i.e. conditionVerified is false. Now use:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"clickBtn"] && conditionVerified) { //just a double checking
        SecondViewController *controller = (SecondViewController *)segue.destinationViewController;
        [controller setManagedObjectContext:self.managedObjectContext];
    }
}

And this would prepare your segue for execution, only if shouldPerformSegueWithIdentifier return YES(true). So you would never have to face any unintended transition if condition is not verified.

Hope it helps.




回答4:


Try This .It worked for me!

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if ([identifier isEqualToString:@"Login"] && condition )
        { 
            return YES;
        }
        else if ([identifier isEqualToString:@"SignUp"])
        {
            return YES;
        }
        else
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"OOPS!!" message:@"Incorrect Username Or Password" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];

            return NO;
        }

    }
     return YES;
}



回答5:


A better way is to use the method shouldPerformSegueWithIdentifier as mentioned here: https://stackoverflow.com/a/12818366/2500457



来源:https://stackoverflow.com/questions/19799623/prepareforsegue-how-can-i-set-an-if-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!