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

Best practices for Storyboard login screen, handling clearing of data upon logout(Storyboard 登录屏幕的最佳实践,在注销时处理数据清除)

本文介绍了Storyboard 登录屏幕的最佳实践,在注销时处理数据清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Storyboard 构建 iOS 应用.根视图控制器是一个标签栏控制器.我正在创建登录/注销过程,它大部分工作正常,但我遇到了一些问题.我需要知道设置这一切的最佳方式.

I'm building an iOS app using a Storyboard. The root view controller is a Tab Bar Controller. I'm creating the login/logout process, and it's mostly working fine, but I've got a few issues. I need to know the BEST way to set all this up.

我想完成以下工作:

  1. 首次启动应用时显示登录屏幕.当他们登录时,转到标签栏控制器的第一个标签.
  2. 之后他们每次启动应用时,检查他们是否已登录,然后直接跳到根标签栏控制器的第一个标签.
  3. 当他们手动单击注销按钮时,显示登录屏幕并清除视图控制器中的所有数据.

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

What I've done so far is set the root view controller to the Tab Bar Controller, and created a custom segue to my Login view controller. Inside my Tab Bar Controller class, I check whether they are logged in inside the viewDidAppear method, and a perform the segue: [self performSegueWithIdentifier:@"pushLogin" sender:self];

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

I also setup a notification for when the logout action needs to be performed: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutAccount) name:@"logoutAccount" object:nil];

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

Upon logout, I clear the credentials from the Keychain, run [self setSelectedIndex:0] and perform the segue to show the login view controller again.

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

This all works fine, but I'm wondering: should this logic be in the AppDelegate? I also have two issues:

  • 他们第一次启动应用程序时,标签栏控制器会在执行 segue 之前短暂显示.我已经尝试将代码移动到 viewWillAppear,但 segue 不会那么早起作用.
  • 当他们注销时,所有数据仍然在所有视图控制器中.如果他们登录到新帐户,则在刷新之前仍会显示旧帐户数据.我需要一种在注销时轻松清除此问题的方法.
  • The first time they launch the app, the Tab Bar Controller shows briefly before the segue is performed. I've tried moving the code to viewWillAppear but the segue will not work that early.
  • When they logout, all the data is still inside all the view controllers. If they login to a new account, the old account data is still displayed until they refresh. I need a way to clear this easily on logout.

我愿意重做这个.我考虑过让登录屏幕成为根视图控制器,或者在 AppDelegate 中创建一个导航控制器来处理所有事情......我只是不确定目前最好的方法是什么.

I'm open to reworking this. I've considered making the login screen the root view controller, or creating a navigation controller in the AppDelegate to handle everything... I'm just not sure what the best method is at this point.

推荐答案

这就是我最终完成所有事情的方法.除此之外,您唯一需要考虑的是 (a) 登录过程和 (b) 存储应用数据的位置(在本例中,我使用了单例).

Here is what I ended up doing to accomplish everything. The only thing you need to consider in addition to this is (a) the login process and (b) where you are storing your app data (in this case, I used a singleton).

如您所见,根视图控制器是我的主选项卡控制器.我这样做是因为在用户登录后,我希望应用程序直接启动到第一个选项卡.(这避免了登录视图临时显示的任何闪烁".)

As you can see, the root view controller is my Main Tab Controller. I did this because after the user has logged in, I want the app to launch directly to the first tab. (This avoids any "flicker" where the login view shows temporarily.)

AppDelegate.m

在这个文件中,我检查用户是否已经登录.如果没有,我推送登录视图控制器.我还处理注销过程,清除数据并显示登录视图.

In this file, I check whether the user is already logged in. If not, I push the login view controller. I also handle the logout process, where I clear data and show the login view.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    // Show login view if not logged in already
    if(![AppData isLoggedIn]) {
        [self showLoginScreen:NO];
    }

    return YES;
}

-(void) showLoginScreen:(BOOL)animated
{

    // Get login screen from storyboard and present it
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    LoginViewController *viewController = (LoginViewController *)[storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
    [self.window makeKeyAndVisible];
    [self.window.rootViewController presentViewController:viewController
                                             animated:animated
                                           completion:nil];
}

-(void) logout
{
    // Remove data from singleton (where all my app data is stored)
    [AppData clearData];

   // Reset view controller (this will quickly clear all the views)
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
   MainTabControllerViewController *viewController = (MainTabControllerViewController *)[storyboard instantiateViewControllerWithIdentifier:@"mainView"];
   [self.window setRootViewController:viewController];

   // Show login screen
   [self showLoginScreen:NO];

}

LoginViewController.m

这里,如果登录成功,我只是关闭视图并发送通知.

Here, if the login is successful, I simply dismiss the view and send a notification.

-(void) loginWasSuccessful
{

     // Send notification
     [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccessful" object:self];

     // Dismiss login screen
     [self dismissViewControllerAnimated:YES completion:nil];

}

这篇关于Storyboard 登录屏幕的最佳实践,在注销时处理数据清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Storyboard 登录屏幕的最佳实践,在注销时处理数据清除

基础教程推荐