Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

iphone - What's the proper way to add a view controller to the view hierarchy?

I have a view controller (A) that loads a view controller (B) and uses it's view in my view hierarchy. If I add B's view to A's view hierarchy, and I don't manually forward events like viewWillAppear, I can't handle them in the B controller. (From the viewWillAppear: docs)

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.

What's the correct way to nest view controllers? (Like NavBarController does it.) If it's just a question of needing to forward a group of events to the nested controller, what are all of the events that I need to forward?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no magic solution here. The correct solution is to manually send these messages.

The viewWillAppear:/viewDidAppear: and viewWillDisappear:/viewDidDisappear: messages are the only messages you need to manually send to the child view controller.

You should implement all four of these methods in the parent view controller and send the same message to the child view controller whenever the parent receives the message and the child is loaded.

In addition, when you add the child view controller's view, you should send the viewWillAppear:/viewDidAppear: messages if the parent's view.window is non-nil. When you remove the view, you should send the viewWillDisappear:/viewDidDisappear: messages if the parent's view.window is non-nil.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...