How to make Segue Pass Data AND ONLY execute if certain criteria is met

别等时光非礼了梦想. 提交于 2019-12-02 07:49:09

问题


I have a Login page with a button. When I click on it, IF and ONLY IF the login was successful, I want to pass data to another class (using properties).

I have tried using a Segue on the Button to pass the data, but the problem is that this Segue always goes to the next View, EVEN when the login is unsuccessful, in which case it should NOT.

 -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {            
if ([[segue identifier] isEqualToString:@"gotoMainPage"] ) {
MainPageController *mpc = [segue destinationViewController];     
mpc.username = @"TEST";    }

How can I make the Segue ONLY go to the destinationController, if the login is successful ?

I am aware that there are other ways to go to another ViewController, such as setting an IBAction on the button, and when login is successful, use instantiateViewControllerWithIdentifier and presentViewController, and I HAVE tried with those too, but the issue here is that, it does NOT pass the data to another class.

Help is appreciated. Thanks


回答1:


You connect the segue from the controller (not a button), and then after your condition is met, call performSegueWithIdentifier in code.




回答2:


Once prepareForSegue:sender: has been called, you can't stop the segue from happening.

You should check for the criteria and call the performSegueWithIdentifier method conditionally.



来源:https://stackoverflow.com/questions/20648490/how-to-make-segue-pass-data-and-only-execute-if-certain-criteria-is-met

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