从iOS应用程序注销

我是这个领域的新手。 我正在开发一个应用程序,用户可以从应用程序中的任何页面注销。

我正在使用此方法进行注销过程。 (从什么是从IOS应用程序注销完美的方式? )

- (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]; } } 

我的问题是如何在执行注销之前closures所有打开的ViewController? 当我执行上述方法时,我点击了注销button的页面在后台保持打开状态。 任何人都可以帮助我相同的。 提前致谢。

首先解散所有的模态UIViewControllers ,你可以做某事。 就像这个方法一样

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

(如在这里看到的: iPhone – closures多个ViewControllers )

代替

 [self.navigationController popToRootViewControllerAnimated:YES]; 

因为这只会popup你的UIViewControllers出你的导航堆栈。

所以对你来说是这样的

 -(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]; } } 

对于一个NSNotification这是一个完美的工作:当用户点击一个注销button时,你可以像这样NSNotification一个自定义通知:

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

然后每个视图/导航/选项卡/任何控制器可以相应地作出反应和“重置”本身。

执行此导航任务不是“注销”button的任务,每个控制器只处理自己的业务,并对这样的应用程序范围的通知作出反应。