I having been mulling for a while an interesting conundrum. I have some code, which is designed to work as a library. It makes no assumptions about any view controller which is active when it is used, and so directly adds things to the view using addSubview: on the application’s main window (view). The initial problem with this approach is that the newly added views have no view controller.
I fixed that by defining a view controller with an appropriate view, but then how to get that displayed. Without any assumptions about the context in which the code is used, the best I could do was instantiate the view controller and then add its view as a subview of the application’s main window as before. An immediate undesirable I discovered was that the new view controller’s viewDidLoad: was never called.
After some fishing around, I found a few useful articles:
Abusing UIViewControllers – presents the case that you should NOT do what I initially do above, force the new view controller’s view into the view hierarchy.
Writing high-quality view controller containers – discusses the same problem and how to write custom view controllers which manage multiple view controllers, like the iOS SplitViewController.
The solution I am going to adopt now is to require the code calling the library to provide a view controller onto which the new view can be pushed using pushModalViewController: