I've read numerous posts about people having problems with viewWillAppear
when you do not create your view hierarchy just right. My problem is I can't figure out what that means.
If I create a RootViewController
and call addSubView
on that controller, I would expect the added view(s) to be wired up for viewWillAppear
events.
Does anyone have an example of a complex programmatic view hierarchy that successfully receives viewWillAppear
events at every level?
Apple's Docs state:
Warning: If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed.
The problem is that they don't describe how to do this. What does "directly" mean? How do you "indirectly" add a view?
I am fairly new to Cocoa and iPhone so it would be nice if there were useful examples from Apple besides the basic Hello World crap.
I've been using a navigation controller. When I want to either descend to another level of data or show my custom view I use the following:
When I do this, I do get the
viewWillAppear
function to fire. I suppose this qualifies as "indirect" because I'm not calling the actualaddSubView
method myself. I don't know if this is 100% applicable to your application since I can't tell if you're using a navigation controller, but maybe it will provide a clue.As no answer is accepted and people (like I did) land here I give my variation. Though I am not sure that was the original problem. When the navigation controller is added as a subview to a another view you must call the viewWillAppear/Dissappear etc. methods yourself like this:
Just to make the example complete. This code appears in my ViewController where I created and added the the navigation controller into a view that I placed on the view.
the .h looks like this
In the nib file I have the view and below this view I have a label a image and the container (another view) where i put the controller in. Here is how it looks. I had to scramble some things as this was work for a client.
Views are added "directly" by calling
[view addSubview:subview]
. Views are added "indirectly" by methods such as tab bars or nav bars that swap subviews.Any time you call
[view addSubview:subviewController.view]
, you should then call[subviewController viewWillAppear:NO]
(or YES as your case may be).I had this problem when I implemented my own custom root-view management system for a subscreen in a game. Manually adding the call to viewWillAppear cured my problem.
I think that adding a subview doesn't necessarily mean that the view will appear, so there is not an automatic call to the class's method that it will
I'm not 100% sure on this, but I think that adding a view to the view hierarchy directly means calling
-addSubview:
on the view controller's view (e.g.,[viewController.view addSubview:anotherViewController.view]
) instead of pushing a new view controller onto the navigation controller's stack.For Swift. First create the protocol to call what you wanted to call in viewWillAppear
Second, create the class
}
Third, make the instance of ForceUpdateOnViewAppear to be the member of the appropriate class that have the access to the Navigation Controller and exists as long as Navigation controller exists. It may be for example the root view controller of the navigation controller or the class that creates or present it. Then assign the instance of ForceUpdateOnViewAppear to the Navigation Controller delegate property as early as possible.