0

I am a newbie in this field. I am working on an app in which user can logout from any page inside the app.

I'm using this method for my log out process. (referred from What is the perfect way to make a logout from IOS app?)

 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
      if (buttonIndex == 0) {
          NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
          [defaults setObject:nil forKey:@"UserId"];
          [defaults synchronize];
          //redirect to login view

          NewClassMoonAppDelegate * appsDelegate =[[UIApplication sharedApplication] delegate];
          LoginViewController *second = [[LoginViewController alloc]initWithNibName:nil bundle:nil];
          [appsDelegate.window setRootViewController:nil];
          [appsDelegate.window setRootViewController:login];
     }
}

My question is how to close all the open ViewController before performing logout? When I m implementing above method, page on which I had clicked logout button remains open in background. Can anyone help me regarding the same. Thanks in advance.

Community
  • 1
  • 1
luckyShubhra
  • 2,606
  • 1
  • 9
  • 18

2 Answers2

1

To dismiss all modal UIViewControllers first, you could do sth. like in this method

-(void)dismissModalStack {
    UIViewController *vc = self.presentingViewController;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:YES completion:NULL];
}

(as seen here:iPhone - dismiss multiple ViewControllers)

instead of

[self.navigationController popToRootViewControllerAnimated:YES];

since this only pops your pushed UIViewControllersout of your navigation stack.

So for you it would be

-(void) actionSheet: (UIActionSheet * ) actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex {
    if (buttonIndex == 0) {
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject: nil forKey: @"UserId"];
        [defaults synchronize];

        UIViewController *vc = self.presentingViewController;
        while (vc.presentingViewController) {
            vc = vc.presentingViewController;
        }
        [vc dismissViewControllerAnimated:YES completion:NULL];

        NewClassMoonAppDelegate * appsDelegate = [[UIApplication sharedApplication] delegate];
        LoginViewController * second = [[LoginViewController alloc] initWithNibName: nil bundle: nil];
        [appsDelegate.window setRootViewController: login];
    }
}
Community
  • 1
  • 1
MarkHim
  • 5,322
  • 5
  • 28
  • 61
  • yeah.. It works.. But now the problem is that LoginViewController page remains open in background. Should i check for the LoginViewController and not close it in dismissModal Stack.. ?? – luckyShubhra Nov 06 '15 at 10:39
  • Don't you want the loginViewController to be visible? What is the target state you wnat to achieve? – MarkHim Nov 06 '15 at 10:40
  • I want loginViewController to be visible. Bt I m getting 2 loginViewController one in foreground as well as one in background. – luckyShubhra Nov 06 '15 at 10:53
  • i ve attached image to my ques. kindly have a look. – luckyShubhra Nov 06 '15 at 10:56
  • if I am omitting this part NewClassMoonAppDelegate * appsDelegate = [[UIApplication sharedApplication] delegate]; LoginViewController * second = [[LoginViewController alloc] initWithNibName: nil bundle: nil]; [appsDelegate.window setRootViewController: login]; its working fine.. Can i omit this part of the code.. ?? – luckyShubhra Nov 06 '15 at 11:16
1

That's a perfect job for a NSNotification: when the user is taps a Logout button, you fire a custom notification like so:

[[NSNotificationCenter defaultCenter] postNotificationName:@"userWillLogoutNotification" object:nil userInfo:nil];

Then every view/navigation/tabbar/whatever controller can react accordingly and "reset" itself.

It's not the job of the Logout button to perform this navigation task, every controller just handles its own business and reacts to application-wide notifications like this.

Cyrille
  • 24,430
  • 12
  • 61
  • 87
  • M new in this field.. may I know how can i access notification in all the pages...?? – luckyShubhra Nov 06 '15 at 10:57
  • 1
    Each controller registers itself to the default NSNotificationCenter, which broadcasts notification to all of its listeners. That's a common approach for this kind of event (one "signal" sent to multiple, potentially unknown listeners) – Cyrille Nov 06 '15 at 14:26