Conditionally following a segue

非 Y 不嫁゛ 提交于 2019-12-17 22:36:27

问题


In my app I have a login screen. When connecting a segue from the login button to the next (table) view controller however, you are always able to proceed, no matter what you type in as login information. How can I check the login information and then decide whether to perform the segue or not, so how to perform the segue conditionally?

Is it possible to achieve this while working with a segue? I know it can be done programmatically, but then I need another navigation controller cause I need a navigation bar..


回答1:


You can wire up your login button or whatever to some IBAction code, decide if the login should proceed and then (if it should) you can use performSegueWithIdentifier: to transition to the new view.

I just wrote another post about using this method, here.




回答2:


On iOS 6, UIViewController's shouldPerformSegueWithIdentifier:sender: method solves this issue without manual triggering segues.




回答3:


  1. Create a manual segue from "login" to say "dashboard"
  2. Give a name for segue identifier, e.g. "showDashboard"
  3. Wire the "Next" button for IBAction
  4. Manually invoke segue with identifier once all the constraints are met.

- (IBAction)nextButtonPressed:(id)sender
{
    if ([self hasAllRequiredFields]) {
        [self performSegueWithIdentifier:@"showDashboard" sender:self];
    } 
}




回答4:


This is belated, but imo it's best to setEnabled:NO whatever is triggering the segue until the segue is "valid". That separates your segue logic from your view logic....



来源:https://stackoverflow.com/questions/7866684/conditionally-following-a-segue

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