iOS状态保存和容器视图 [英] iOS state preservation and container views

查看:122
本文介绍了iOS状态保存和容器视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在故事板中有一个使用容器视图的视图控制器。两者都具有恢复标识符集。父母正在被保存并恢复正常。然而,孩子不是。在子视图控制器上都没有调用 -encodeRestorableStateWithCoder: -decodeRestorableStateWithCoder:

I have a view controller in a storyboard that is using a container view. Both have restoration identifiers set. The parent is being saved and restored just fine. The child however is not. Neither -encodeRestorableStateWithCoder: or -decodeRestorableStateWithCoder: are being called on the child view controller.

保存使用视图容器创建的子视图控制器的正确方法是什么?我可以将子视图控制器保存在父 -encodeRestorableStateWithCoder:中,这将导致它被保存,但我没有办法在恢复期间使用它。

What's the correct way to save child view controllers that are created with a view container? I can save the child view controller in the parents -encodeRestorableStateWithCoder:, which will cause it to be saved, but I don't have a way of using it during a restore.

推荐答案

容器视图控制器不会自动保存对任何包含的子视图控制器的引用。如果要实现自定义容器视图控制器,如果要保留它们,必须自己对子视图控制器对象进行编码

我找到了一些简单的规则:

There are simple rules that i found:

1.嵌入式(子)视图控制器应该已经创建并在状态保存过程中添加到父视图控制器。因此,如果您使用storyboard,则无需执行任何操作,否则您必须实例化子视图控制器并手动添加:

1.Embedded(child) view controller should already be created and added to parent view controller at the state preservation process. So, do not have to do anything if you use storyboard otherwise you'll have to instantiate child view controller and add it manually:

-(void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"Did load");
    MyChildViewController *childViewController = [MyChildViewController new];
    [self addChildViewController:childViewController];
    [childViewController didMoveToParentViewController:self];
    self.childVC = childViewController;
}

您可以在 -viewDidLoad添加子视图或更高版本。使用 self.childVC.view.frame = [self frameForChildController]; [self.view addSubview:self.childVC.view]; for this。

You can add child view at -viewDidLoad or later. Use self.childVC.view.frame = [self frameForChildController]; [self.view addSubview:self.childVC.view]; for this.

2.您无需保存子视图控制器父的 -encodeRestorableStateWithCoder:他自己,但你应该使用 -encodeObject:forKey 向该对象编码引用: code>。如果您有参考,您可以这样做:

2.You no need to save the child view controller in the parent's -encodeRestorableStateWithCoder: himself, but you should encode a reference to that object using -encodeObject:forKey:. If you have reference you can do it like this:

-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    NSLog(@"Encode");
    UIViewController *childViewController = self.childVC;
    [coder encodeObject:childViewController forKey:@"ChildVC"];
    [super encodeRestorableStateWithCoder:coder];
}

参见 https://stackoverflow.com/a/13279703/2492707 如果您使用Storyboard,则可以获得对子VC的引用。或者你可以写这样简单的东西:

see https://stackoverflow.com/a/13279703/2492707 to get reference to child VC if you use Storyboard. Or you can write something simple like this:

-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    NSLog(@"Encode");
    UIViewController *childViewController = [self.childViewControllers objectAtIndex:0]; //[self.childViewControllers lastObject];
    [coder encodeObject:childViewController forKey:@"ChildVC"];
    [super encodeRestorableStateWithCoder:coder];
}

3. 应该已经创建了嵌入式(子)视图控制器在状态恢复过程中添加到父视图控制器。所以,如果你在第一段中做了所有事情,那么这里没有更多的事情要做了。

3.Embedded(child) view controller should already be created and added to parent view controller at the state restoration process. So, if you did everything in the first paragraph, there is nothing more to do here.

4.但是,在这种情况下,我们不解码子视图控制器我们可以,但事实上我们不需要它.MyChildViewController对象将恢复它自己的状态。我们只编码这个引用,以便让运行时将链转向MyChildViewController实例并对它进行保存和恢复。

4."In this case, however, we do not decode child view controller. We could, but in fact we don't need it.The MyChildViewController object will restore its own state. We only encoded this reference in order to get the runtime to walk the chain down to the MyChildViewController instance and do save-and-restore on it".

-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
    NSLog(@"Decode");
    [super decodeRestorableStateWithCoder:coder];
}

这本书帮助我了解容器视图的状态保存。另外看看本书的一个很好的例子

This book helps me for understanding state preservation with container views. Also look for a good example for this book

这篇关于iOS状态保存和容器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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