iOS 5 Segue not working after the first execution
Asked Answered
M

1

2

I'm creating an iOS5 application using the storyboard features. The basic structure is:

LoginScreen ---(segue)--> MyScreen ---(press on logout)------(segue back to login screen)-->LoginScreen

it's pretty simple. The way I manage the first segue is:

- (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
  [self performSegueWithIdentifier:@"showHomeScreen" sender:nil];
}

Now,the strange thing is that the segue is correctly performed the first time,but,when I come back to the login screen after a logout the performSegueWithIdentifier: doesn't work anymore (no error messages,simply nothing happens). Not sure what's going on. Which might be the problem?

I attach a screenshot of the storyboard..you can see the loop in the top-right corner: enter image description here

thanks a lot!

Claus

Machinist answered 14/2, 2012 at 12:25 Comment(0)
D
4

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 :)

Storyboard

Differentiate answered 14/2, 2012 at 13:13 Comment(6)
Hi Eljay,thanks for your answer. Yes,please a working example would be great and also useful for who is reading the thread. Could you please post it? I've already tried an approach with the delegate for dismissing the viewController but in my case the are due navigation controllers (tab+nav) and it got a bit complicated.Machinist
Besides that,which is the problem in having more than one Segue?Machinist
Not a problem with having more than one segue, but in your case, there should only be one. The LoginVC should not be connected to the NavController. Will modify the answer to include an example in a few minutesDifferentiate
great thanks a lot! Still I don't understand why the performSegueWithIdentifier: works only the first time.Machinist
Hi ElJay, just on observation. I've kept on working on the example. I'm checking for my authorization token in the viewWillAppear: method //check if the token is set,if not trigger the Login screen NSString* token = nil; NSUserDefaults* userPref = [NSUserDefaults standardUserDefaults]; token = [userPref objectForKey:@"AuthToken"]; if (token == nil) { NSLog(@"Token not present,Login required!"); [self performSegueWithIdentifier:@"sLogin" sender:nil]; } [super viewWillAppear:YES];` the app works fine,only that the very first timeMachinist
when the token is still not present the Dashboard is displayed for few seconds. The rest is fine but would be possible to perform this transition in a transparent way so the user doesn't perceive it? Yeah...I know...I'm quite a newbie :)Machinist

© 2022 - 2024 — McMap. All rights reserved.