0

Referring to this question: addChildViewController alternative for iOS 4.3

My app requires support for 4.3 so I am unable to take the approach mentioned in the Apple guidelines for container programming - http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

Directly swapping the views of each ViewController works for me currently, however, when it comes to pushing a new View Controller, these 'children' do not have a reference to the container's self.navigationController so they are unable to push a new controller on the stack.

What is the best practice for being able to access the UINavigationController of the container class in iOS 4.3. It is a shame I am being forced to support 4.3 as the childViewController pattern outlined in the Apple documentation is exactly what I'd need otherwise!

Thanks,

Tim.

Community
  • 1
  • 1
Tim
  • 8,932
  • 4
  • 43
  • 64

1 Answers1

0

In iOS 4 and before, you are not supposed to create your own UIViewController hierarchy, so you have to manipulate views directly as subviews of a UIViewController's view.

However, if you do this correctly, there is still a UIViewController hierarchy and it does still work. There is no circumstance in which you "do not have a reference" to the UINavigationController. A view has a superview, which is another view. A UIViewController's view has a reference to the UIViewController (as its nextResponder). The UIViewController has a reference to its parent UINavigationController if there is one.

If that's not the case, you're doing something very wrong with view controllers.

Apple's documentation says this (for iOS 4 and before), and you should take it very seriously:

You should not use multiple custom view controllers to manage different portions of the same view hierarchy. ... If you want to divide a view hierarchy into multiple subareas and manage each one separately, use generic controller objects (custom objects descending from NSObject) instead of view controller objects to manage each subarea. Then use a single view controller object to manage the generic controller objects.

The rule in iOS 4 and later remains the same: do not use a UIViewController except as part of a legitimate UIViewController hierarchy, where the view controller's view is in exactly the same hierarchy, as in this diagram from my book:

enter image description here

Observe that the view controllers are parent and child and that their views are superview and subview. That is how to use a view controller hierarchy. If you're doing things in iOS 4 that would violate this, don't. Use views but not view controllers.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I am implementing my own container view controller that works in a similar way to the UITabBarController. A UIViewController with an NSArray of UIViewControllers, and I have segmented control embedded in the UIViewController in order to swap between the controllers within the array. The issue is when it comes to pushing a new view controller within the children, the children do not have a reference to the parents self.navigationController. However, when using childViewControllers in iOS5 I do, but obviously this isn't supported in 4.3 – Tim Mar 22 '13 at 20:56
  • Right, well, what my answer above is saying is, don't do that. Don't manipulate view controllers in that way in iOS 4. Just manipulate views, within the view of the legitimate view controller. – matt Mar 22 '13 at 21:01
  • Such a frustrating issue as this is a primarily iOS 5+ app, but are being forced to support iOS4 as a secondary issue. How is a UITabController implemented in iOS4 then if you're not supposed to manage multiple VCs within one another? – Tim Mar 22 '13 at 21:14
  • Apple's built-in parent controllers can do it (UITabController, UINavigationController) but you can't. That is the problem they solved in iOS 5 and iOS 6 with custom parent controllers! – matt Mar 22 '13 at 21:31