iOS state preservation and container views

前端 未结 2 1848
清歌不尽
清歌不尽 2021-02-01 20:33

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 howev

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 20:58

    I think the answer is in the documentation It is said:

    " The UIViewController class saves a reference to the presented view controller and the storyboard (if any) that was used to create the view controller. The view controller also asks the views in its view hierarchy to save out any relevant information. However, this class does not automatically save references to any contained child view controllers. If you are implementing a custom container view controller, you must encode the child view controller objects yourself if you want them to be preserved."

    So you could do something like that:

    -(void)encodeRestorableStateWithCoder:(NSCoder *)coder {
        [super encodeRestorableStateWithCoder:coder];
        [self.myChildViewController encodeRestorableStateWithCoder:coder];
    }
    
    -(void)decodeRestorableStateWithCoder:(NSCoder *)coder {
        [super decodeRestorableStateWithCoder:coder];
        [self.myChildViewController decodeRestorableStateWithCoder:coder];
    }
    

    And in MyChildViewController do not call super :)

提交回复
热议问题