注销时重置情节提要 [英] Resetting Storyboard on Logout

查看:76
本文介绍了注销时重置情节提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用情节提要的IOS 5.1 Web客户端应用程序.我的操作之一是注销",在此期间,我想将根视图重置为由Storyboard的根视图创建的初始视图. (登录时,会根据您的身份删除或添加一些视图项;注销时,我想将其重置为在情节提要中指定的默认值.)

I am building an IOS 5.1 web client app that uses a storyboard. One of my actions is "logout", during which I want to reset my root view to the initial view created by the root view of the Storyboard. (When you log in, some view items are removed or added based on who you are; when you log out, I want to reset them to their default values, which I've specified in the storyboard.)

我意识到我可以以编程方式重置/重新添加所有元素,但是情节提要有什么用?我认为必须有一种方法可以通过重新加载视图文件来恢复原样,对吧?

I realize that I could programmatically reset/re-add all of the elements, but then what good is the storyboard? I figure there's got to be a way to get back to square one by reloading the view file, right?

推荐答案

我发现以下方法适用于我.请注意,我使用ARC,但是不确定这是否对解决方案有很大影响.首先,在应用程序委托类的application:didFinishLaunchingWithOptions:中,使用以下代码行捕获初始的Storyboard实例:

I found the following approach works for me. Please note that I use ARC, unsure if this has much bearing on the solution however. First, in the app delegate class, in application:didFinishLaunchingWithOptions: I capture the inital Storyboard instance with the following line of code:

_initalStoryboard = self.window.rootViewController.storyboard;

(显然有一个实例变量UIStoryboard* _initalStoryboard;)

(Obviously there is an instance variable UIStoryboard* _initalStoryboard;)

然后我在我的应用程序委托中定义了以下函数:

Then I have the following function defined in my app delegate:

- (void)resetWindowToInitialView
{
    for (UIView* view in self.window.subviews)
    {
        [view removeFromSuperview];
    }

    UIViewController* initialScene = [_initalStoryboard instantiateInitialViewController];
    self.window.rootViewController = initialScene;
}

请注意for循环,该循环将从window中删除所有子视图. UIWindow rootViewController文档指出:

Please note the for in loop which removes all subviews from window. The UIWindow rootViewController documentation states:

如果窗口具有现有视图层次结构,则旧视图为 在安装新产品之前将其删除.

If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

但是我发现情况并非如此...因此,我在分配新的rootViewController之前明确地删除了现有视图.使用这种方法,我还没有发现任何令人担忧的副作用或内存泄漏.我绝不是UIKit魔术的专家,所以如果您打算自己使用它,我建议您测试一下并重新测试该解决方案.干杯

However I did not find this to be the case... so I remove the existing views myself explicitly before assigning a new rootViewController. I have not found any worrying side effects or memory leaks using this method. I am by no means an expert on the magic of UIKit so I would suggest you test test and retest this solution if you plan to use it yourself. Cheers

这篇关于注销时重置情节提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆