Storyboard登录屏幕的最佳做法,在注销时处理数据清除

Storyboard登录屏幕的最佳做法,在注销时处理数据清除

我正在使用Storyboard构建iOS应用程序。根视图控制器是一个标签栏控制器。我正在创建登录/注销过程,它基本上工作正常,但我有一些问题。我需要知道设置这一切的最好方法。

我想完成以下任务:

  1. 首次启动应用程序时显示登录屏幕。登录后,转到选项卡栏控制器的第一个选项卡。

  2. 任何时候他们在此之后启动应用程序,检查他们是否已登录,并直接跳到根选项卡栏控制器的第一个选项卡。

  3. 当他们手动单击注销按钮时,显示登录屏幕,并清除视图控制器中的所有数据。

到目前为止我所做的是将根视图控制器设置为Tab Bar Controller,并为我的Login视图控制器创建了一个自定义segue。在我的Tab Bar Controller类中,我检查它们是否在viewDidAppear方法内登录,然后执行segue:[self performSegueWithIdentifier:@"pushLogin" sender:self];

我还设置了需要执行注销操作的通知: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutAccount) name:@"logoutAccount" object:nil];

注销后,我清除Keychain中的凭据,运行[self setSelectedIndex:0]并执行segue以再次显示登录视图控制器。

这一切都很好,但我想知道:这个逻辑应该在AppDelegate吗?我还有两个问题:

  • 第一次启动应用程序时,Tab Bar Controller会在执行segue之前短暂显示。我已经尝试过移动代码viewWillAppear但是segue不会那么早。

  • 当他们注销时,所有数据仍然在所有视图控制器中。如果他们登录到新帐户,旧帐户数据仍会显示,直到刷新为止。我需要一种方法在注销时轻松清除它。

我愿意改变它。我已经考虑将登录屏幕设置为根视图控制器,或者在AppDelegate中创建导航控制器来处理所有事情......我只是不确定此时最好的方法是什么。


阿波罗的战车
浏览 653回答 3
3回答

三国纷争

在您的didFinishLaunchingWithOptions中的appDelegate.m中//authenticatedUser: check from NSUserDefaults User credential if its present then set your navigation flow accordinglyif (authenticatedUser) {    self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];        }else{    UIViewController* rootController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LoginViewController"];    UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:rootController];    self.window.rootViewController = navigation;}在SignUpViewController.m文件中- (IBAction)actionSignup:(id)sender{    AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];    appDelegateTemp.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];}在文件MyTabThreeViewController.m中- (IBAction)actionLogout:(id)sender {    // Delete User credential from NSUserDefaults and other data related to user    AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];    UIViewController* rootController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LoginViewController"];    UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:rootController];    appDelegateTemp.window.rootViewController = navigation;}Swift 4版本应用委托中的didFinishLaunchingWithOptions假设您的初始视图控制器是在TabbarController中签名的。if Auth.auth().currentUser == nil {        let rootController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "WelcomeNavigation")        self.window?.rootViewController = rootController    }    return true在注册视图控制器中:@IBAction func actionSignup(_ sender: Any) {let appDelegateTemp = UIApplication.shared.delegate as? AppDelegateappDelegateTemp?.window?.rootViewController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateInitialViewController()}MyTabThreeViewController //Remove user credentialsguard let appDel = UIApplication.shared.delegate as? AppDelegate else { return }        let rootController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "WelcomeNavigation")        appDel.window?.rootViewController = rootController
打开App,查看更多内容
随时随地看视频慕课网APP