【问题标题】:How should I be resetting my root view controller for logout我应该如何重置我的根视图控制器以注销
【发布时间】:2017-02-02 17:15:23
【问题描述】:

我的应用程序是用我的登录屏幕作为初始 VC 实例化的。当用户成功登录时,我实例化一个导航控制器并将 homeVC 设置为根视图控制器。登录后,用户可以从应用程序的任何页面访问他们的“我的个人资料”信息,也可以退出。

“返回”到登录视图控制器的最佳方式是什么?我想确保从内存中删除任何 VC,但由于登录 VC 存在于导航控制器之外,我不能简单地跳回根视图控制器。

感谢任何指导。

【问题讨论】:

    标签: ios objective-c uinavigationcontroller


    【解决方案1】:

    简短回答,如果您通过设置窗口的根从登录 UI 到应用程序的主 UI,那么这是返回的好方法。

    // in some view controller in your app when you need to change to the login UI
    UIStoryboard *storyboard = [self storyboard];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MyLoginVCIdentifier"];
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    window.rootViewController = vc;
    

    更长的答案,我有时会使用一个视图控制器,它唯一的工作就是管理这个,称之为LaunchViewController

    在我的 main.storyboard 中,我创建了一个 LaunchViewController 的实例并将“Is Initial View Controller”设置为 true。

    这个 VC 不需要 UI,因为它唯一的工作就是在它出现时立即替换它自己。但是,由于我什至不希望在 LaunchScreen.storyboard 之后出现瞬间闪烁,因此有时我会这样做以将这个 vc 的视图与启动故事板视图重叠,但这部分是可选的....

    // LaunchViewController.m
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIStoryboard *storyboard = [self.class storyboardWithKey:@"UILaunchStoryboardName"];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LaunchVC"];
        [self.view addSubview:vc.view];
    }
    
    // a convenience method to get a storyboard from the bundle by key
    + (UIStoryboard *)storyboardWithKey:(NSString *)key {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *storyboardName = [bundle objectForInfoDictionaryKey:key];
        return [UIStoryboard storyboardWithName:storyboardName bundle:bundle];
    }
    

    回到您的问题,我的 LaunchViewController 提供了一种方法,该方法在给定故事板标识符的情况下呈现(带有您选择的动画)主故事板视图控制器...

    // LaunchViewController.m
    + (void)presentUI:(NSString *)identifier {
        UIStoryboard *storyboard = [self storyboardWithKey:@"UIMainStoryboardFile"];
        UINavigationController *vc = [storyboard instantiateViewControllerWithIdentifier:identifier];
    
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        window.rootViewController = vc;
    
        [UIView transitionWithView:window
                          duration:0.3
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:nil
                        completion:nil];
    }
    

    这样,我们可以给LaunchViewController 任意数量的公共方法,比如...

    + (void)presentLoginUI {
        [self presentUI:@"IdentifierOfMyLoginViewController"];
    }
    
    + (void)presentMainAppUI {
        [self presentUI:@"IdentifierOfMyMainAppViewController"];
    }
    

    由于系统窗口只有一个指向根视图控制器的指针,而您在 presentUI: 中替换了该指针,ARC 将为您清理整个丢弃的 UI。

    【讨论】:

    • 这是一个非常好的答案。
    • 我们有什么方法可以合并一个简单的动画,同时将它带回根视图控制器?
    • @MrPool,是的。 transitionWithView 为标准 Vic 演示动画提供了动画参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2015-12-28
    • 2014-07-27
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多