3

I would like to use the same functionality of addChildViewController, but for the version 4.3 (addChildViewController is only available in version 5). Thanks in advance.

Luis Andrés García
  • 5,852
  • 10
  • 43
  • 57

3 Answers3

2

Although it's not recommended, you can create an instance of a UIViewController access it's view property and set it as a subview of the main view your main UIViewController is managing. It works, and I never experienced a problem with this.

That said, the recommended way is to have a NSObject subclass to act as your sub-controller, and not use a UIViewController as it has special behavior.

pgb
  • 24,813
  • 12
  • 83
  • 113
  • In addition to this, you can keep track of the added view controllers by referencing them from a retained NSArray (full init) or adding them to retained NSMutableArray (lazy init) – shawnwall Feb 13 '12 at 20:30
0

Something similar is explained in iOS: different addSubview behavior between iOS 4.3 and 5.0.

I have solved it by adding these lines:

NSMutableArray *controladores=[[NSMutableArray alloc]init];
        [controladores addObject:myController];
        if ([[[UIDevice currentDevice] systemVersion] compare:@"5.0"] == NSOrderedAscending) {
            [tabBar setViewControllers:controladores animated:YES];
        }
Community
  • 1
  • 1
Luis Andrés García
  • 5,852
  • 10
  • 43
  • 57
  • 2
    I would suggest against checking for the OS version by accessing the `systemVersion` property. Instead, you should check for the presence of the `setViewControllers:` selector, by doing something like: `if ([tabBar respondsToSelector:@selector(setViewControllers:)])`. – pgb Feb 14 '12 at 17:07
0

I assume the reason you want to do this is for ipad application, as Apple state that a single view controller should "generally" (and before ipad existed they stated "always") control a whole screen full of views.

This really only changed with the extra screen real estate of the ipad when Apple made their own container view controller (splitViewController) which ultimately led to Apple allowing us to create our own container view controllers in ios5.

If your app is an iphone app then I (and Apple) would seriously question your need for view controller containment.

ader
  • 5,403
  • 1
  • 21
  • 26