I need to create custom container viewcontroller with label and 2 states A and B(each state presented by child viewcontroller and swap each other). Both children viewcontrollers instantiates when parent container creates and currentViewController is viewControllerA by default. So i have something like this in container viewcontroller:
@property(weak, nonatomic) id currentViewController;
@property(strong, nonatomic) id viewControllerA;
@property(strong, nonatomic) id viewControllerB;
@property(strong, nonatomic) UILabel* label;
...
// Question here
- (void)viewDidLoad
{
[super viewDidLoad];
self.viewControllerA = [ViewControllerA new];
self.viewControllerB = [ViewControllerB new];
self.currentViewController = self.viewControllerA;
[self populateCurrentViewController];
}
- (void)populateCurrentViewController
{
[self addChildViewController:self.currentViewController];
[self.view addSubview:self.currentViewController.view];
[self.currentViewController didMoveToParentViewController:self];
}
- (void)goToState: (UIViewController*)stateController
{
if(self.currentViewController != stateController)
{
[self.currentViewController willMoveToParentViewController:nil];
[self addChildViewController:state];
[self transitionFromViewController: self.currentViewController toViewController: stateController
duration: 0.25 options:UIViewAnimationOptionTransitionFlipFromLeft
animations:nil
completion:^(BOOL finished) {
[self.currentViewController removeFromParentViewController];
[stateController didMoveToParentViewController:self];
}];
}
}
- (void)goToStateA
{
[self goToState: self.viewControllerA];
self.label.text = @"A";
}
- (void)goToStateB
{
[self goToState: self.viewControllerB];
self.label.text = @"B";
}
I need to preserve state of container viewcontroller. The question is where i must instantiate and add children viewcontrollers on first creating my container viewcontroller and on restoration process? Where i must restore children viewcontrollers?
I found many candidates:
-(void)initWithCoder
-(void)awakeFromNib
-(void)viewDidLoad
-(void)viewWillAppear
and
-(void)decodeRestorableStateWithCoder
for restoration process.
A couple of thoughts on this subject. If use initWithCoder
and viewDidLoad
for creating children viewcontrollers and adding current viewcontroller as a child to container viewcontroller, then after restoration process i need to change current viewcontroller and swap it if necessary, what is wrong i think. If use viewWillAppear
for adding current viewcontroller, then it is normal for restoration process, but this method may be invoked several times.
I think there are two ways to restore restore children viewcontrollers:
Instantiate in
viewDidLoad
and restore in-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
like[self.viewControllerA decodeRestorableStateWithCoder:coder]
(i saw it here - https://stackoverflow.com/a/16647606/2492707)Full restore in
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
likeself.viewControllerA = [coder decodeObjectForKey: @"AKey"];
Which way is better?
Understanding of the recovery process of the UINavigationController (where visibleViewController is populated) can help me too.
Thanks in advance.