viewWillAppear not invoked for subview controller
Solution 1
The application isn't aware of the subview's view controller if you do this, you need to introduce view controller containment to make the root view controller aware. Doing so will handle any events like this.
Because loadView
could be called more than once pre iOS 6, I'd advise creating the view controller within init
, and then add the subview within loadView
. It should be like this:
- (id)init {
...
self.doneButtonViewController = [[DoneButtonViewController alloc] init];
[self addChildViewController:self.doneButtonViewController];
[self.doneButtonViewController didMoveToParentViewController:self];
...
}
- (void)loadView {
...
[self.view addSubview:self.doneButtonViewController.view];
...
}
See "Implementing a Container View Controller" at http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
Solution 2
As for me, adding child view controller in parent view controller can solve the problem that "viewWillAppear" of the child view controller not get called.
Comments
-
Hot Licks almost 2 years
I have an odd case -- a view controller that creates its own view in
loadView
and which is then added to an existing view.Here is the code that creates and adds the VC:
self.doneButtonViewController = [[DoneButtonViewController alloc] init]; [self.view addSubview:self.doneButtonViewController.view];
This code is executed in
viewDidLoad
of the "parent" VC.The odd thing is that the
viewWillAppear
method of the added VC is never invoked (nor isviewDidAppear
), but theviewWillDisappear
method of the added VC is invoked (at the appropriate time), just as one would expect.Any clue as to why
viewWillAppear
is not getting invoked? -
jrturton over 11 yearsObviously the loadView implementation would need to construct the rest of the parent view controller's view as well, but this is the right answer. The code can be in viewDidLoad if you are using storyboards or xibs to make the parent VC.
-
Hot Licks over 11 yearsProblem is, I'm trying to come up with a minimal scheme for adding this -- basically invoke one factory method that creates the VC and adds the view.
-
WDUK over 11 yearsYou'd have to pass in a reference to the view controller it's being added to, and use the same process. If you worry this may happen more than once, then either have checking code inside the factory to not add duplicates, or have
removeFromParentViewController:
at the ready if you need to remove it. -
Hot Licks over 11 yearsI'd tried addChildViewController, but I didn't realize I needed addSubview as well. With the pair it works more or less as expected.
-
WDUK over 11 yearsAll addChildViewController does is create a link between the two view controllers, it does nothing regarding the views themselves. This link allows events, such as viewWillAppear: to be propagated down to the child view controllers, so they in turn can manage their own view. The actual adding of the views themselves need to be added the traditional way as well.