iOS 5 Segue not working after the first execution

本秂侑毒 提交于 2019-11-30 16:28:43

It looks like that LoginVC is connected to more than one Segue.

The best way to handle that Login process is to use a delegate for the Login ViewController. Then in the main VC, you check credentials or whatever and if needed call the performSegue for the LoginVC. When the Login is successful, you call the delegate method and the Main VC will dismiss the modal view. The LoginVC really shouldn't be part of the navigation or connected to any other Segues other than the one from the Main VC. I have a complete example if you need it, but this is easy to implement using delegate methods.

Here ya go: LoginViewController.h:

@protocol LoginViewControllerDelegate
    -(void)finishedLoadingUserInfo;
@end

@interface LoginViewController : UIViewController <UITextFieldDelegate>{
    id <LoginViewControllerDelegate> delegate;
}

LoginViewController.m:

@synthesize delegate;

- (void) onResponse:(NSMutableDictionary *)response {
  NSLog(@"Login successful,token received");
  // if the Login was successful,store the token 
  NSUserDefaults* userPref = [NSUserDefaults standardUserDefaults];    
  [userPref setObject:[response objectForKey:@"Token"] forKey:@"AuthToken"];
  [userPref synchronize];
  //..and let the user getting in
  [delegate finishedLoadingUserInfo];
}

In the Dashboard VC .m file:

#pragma mark - LoginViewController Delegate Method
-(void)finishedLoadingUserInfo
{    
    // Dismiss the LoginViewController that we instantiated earlier
    [self dismissModalViewControllerAnimated:YES];

    // Do other stuff as needed
}

So the gist is to check for credentials when the app loads and if needed, call (in the Dashboard VC):

[self performSegueWithIdentifier:@"sLogin" sender:nil];

Then in the prepareForSegue method (in the Dashboard VC):

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"sLogin"]) {
        LoginViewController *livc = segue.destinationViewController;
        livc.delegate = self; // For the delegate method
    }
}

Make sure to name the Segue sLogin or this won't work :)

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