Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewWillAppear not invoked for subview controller

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 is viewDidAppear), but the viewWillDisappear 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?

like image 966
Hot Licks Avatar asked Nov 27 '12 22:11

Hot Licks


People also ask

Why does viewWillAppear not get called when an app comes back from the background?

In other words, if someone looks at another application or takes a phone call, then switches back to your app which was earlier on backgrounded, your UIViewController which was already visible when you left your app 'doesn't care' so to speak -- as far as it is concerned, it's never disappeared and it's still visible ...

Is viewWillAppear always called?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.

What is IOS viewWillAppear?

Notifies the view controller that its view is about to be added to a view hierarchy.

What is viewWillAppear in Swift?

viewWillAppear(_:)This method is called when the view is about to appear (be shown on the screen), but before it is actually displayed. This method allows you to do some further customization. Unlike the 'viewDidLoad()', 'viewWillAppear(_:)' can be called multiple times.


2 Answers

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

like image 146
WDUK Avatar answered Sep 29 '22 12:09

WDUK


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.

like image 36
user1806228 Avatar answered Sep 29 '22 12:09

user1806228