【问题标题】:Logout from iOS App从 iOS 应用程序注销
【发布时间】:2016-02-07 10:48:59
【问题描述】:

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

我在注销过程中使用这种方法。 (引用自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];
     }
}

我的问题是如何在执行注销之前关闭所有打开的 ViewController?当我实现上述方法时,我单击注销按钮的页面在后台保持打开状态。任何人都可以帮助我解决同样的问题。提前致谢。

【问题讨论】:

  • 您应该在注销成功后执行“关闭”堆栈中的所有 VC。只需致电[self.navigationController popToRootViewControllerAnimated:YES];。这将弹出导航堆栈中的所有现有 VC,并将您带回您的根 Vc
  • 视图控制器留在后台?你是什​​么意思。它仍然可见吗?
  • 谢谢@NSNoob.. 这个应用程序没有使用导航控制器。有没有其他办法..??
  • 是的@MarkHim,当我在模拟器上运行它时,该页面仍然可见。
  • @luckyShubhra 你是在模态地表达你的观点吗?

标签: ios


【解决方案1】:

首先要关闭所有模态UIViewControllers,您可以这样做。喜欢这个方法

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

(如这里所示:iPhone - dismiss multiple ViewControllers

而不是

[self.navigationController popToRootViewControllerAnimated:YES];

因为这只会将您推送的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];
    }
}

【讨论】:

  • 是的.. 它可以工作.. 但现在的问题是 LoginViewController 页面在后台保持打开状态。我应该检查 LoginViewController 而不是在dismissModal Stack中关闭它吗??
  • 您不想让 loginViewController 可见吗?你想达到的目标状态是什么?
  • 我希望 loginViewController 可见。顺便说一句,我得到 2 个 loginViewController 一个在前台,一个在后台。
  • 我已将图片附加到我的问题中。请看一下。
  • 如果我省略这部分 NewClassMoonAppDelegate * appsDelegate = [[UIApplication sharedApplication] delegate]; LoginViewController * second = [[LoginViewController alloc] initWithNibName: nil bundle: nil]; [appsDelegate.window setRootViewController: 登录];它工作正常..我可以省略这部分代码.. ??
【解决方案2】:

这对于NSNotification 来说是一项完美的工作:当用户点击注销按钮时,您会触发一个自定义通知,如下所示:

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

然后每个视图/导航/标签栏/任何控制器都可以做出相应的反应并“重置”自己。

执行此导航任务不是注销按钮的工作,每个控制器只处理自己的业务并对应用程序范围内的通知做出反应。

【讨论】:

  • M new in this field..我可以知道如何访问所有页面中的通知...??
  • 每个控制器都将自己注册到默认的 NSNotificationCenter,它会向其所有侦听器广播通知。这是此类事件的常用方法(将一个“信号”发送给多个可能未知的侦听器)
猜你喜欢
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 2017-03-16
  • 2013-06-10
相关资源
最近更新 更多