7

Has anyone else encountered this? The following code reports "YES" when running on the iOS 4 simulator but according to the Apple docs the method addChildViewController is only available on iOS 5 and later. This doesn't seem like the correct behavior, is this a bug?

if([UIViewController instancesRespondToSelector:@selector(addChildViewController:)]) {
    NSLog(@"YES"); 
} else {
    NSLog(@"NO");
}
Hua-Ying
  • 3,166
  • 4
  • 23
  • 25
  • 1
    I'm not sure why this happens but I confirmed I see the same thing in XCode 4.2. I was mindful to set my deployment target to 4.0 as well. Doing the same test for automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers for example returns NO. – Joel Nov 15 '11 at 21:20
  • does it display a warning when trying to run it? It is possible that that method was included in 4.0 but is private, though usually private methods have _ infront of them...so seems odd – Daniel Nov 15 '11 at 22:09
  • It could be a private API that Apple was using and testing in iOS 4. Now that it's production ready, it may have just been released to public consumption with iOS 5. I don't know for sure, just spit balling here. – Ryan Wersal Nov 17 '11 at 22:36
  • This happens from time to time. A lot of the `UIGestureRecognizer` methods were available in iPhone OS 3.1 before they were made publicly available in 3.2. – Mark Adams Jan 15 '12 at 21:00
  • Does it behave properly? Does it send viewWill*, viewDid*? And is it actually safe then to use it without checks on iOS4? – Anton Feb 07 '12 at 13:07
  • The behavior is the same on an iPad device running 4.3.5, i.e. addChildViewController is available. – tribalvibes Feb 12 '12 at 23:25

3 Answers3

3

I think this is a bug. Calling the addChildViewController seems to run without any warning or error too.

I wrote the following viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    MyChildView *aChildViewController = [[MyChildView alloc] initWithNibName:@"MyChildView" bundle:nil];

    // Do any additional setup after loading the view, typically from a nib.
    SEL mySelector = @selector(addChildViewController:);
    if([UIViewController instancesRespondToSelector:mySelector] == YES) {
        NSLog(@"YES addChildViewController:"); 
        [self addChildViewController:aChildViewController];
    } else {
        NSLog(@"NO addChildViewController:");
    }

    if([UIViewController instancesRespondToSelector:@selector(automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers)] == YES) {
        NSLog(@"YES automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers");

    } else {
        NSLog(@"NO automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers");
    }
}

In the iOS 4.3 Simulator I see following output. Both messages are restricted to IOS 5.0 and higher. It appears addChildViewController is responding in the 4.3 simulator incorrectly. I don't have 4.3 device to test on an actual device.

2011-11-18 09:55:12.161 testViewFunctionality[873:b303] YES addChildViewController:
2011-11-18 09:55:12.162 testViewFunctionality[873:b303] NO automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers

In the iOS 5.0 Simulator both respond which is correct behavior.

2011-11-18 09:59:31.250 testViewFunctionality[932:f803] YES addChildViewController:
2011-11-18 09:59:31.252 testViewFunctionality[932:f803] YES automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers

I'm using XCode 4.2 on Lion. When I look through UIViewController.h on the 4.3 Simulator's framework there is no mention of addChildViewController: or automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers but the only SDK included is 5.0.

enter image description here

I suppose that if you wanted to be cautious you could test the running iOS version on the running device. See How to check iOS version?

Community
  • 1
  • 1
Joel
  • 2,928
  • 2
  • 24
  • 34
  • Don't do @selector checks! Apple often already has a *slightly* different private API ready before it releases it to the public. Always use checks for CoreFoundation-Version. Much safer. (e.g. a lot of apps crashed with the UIScreen scale in 3.2 that worked quite different than >=4.0 public scale property) – steipete Dec 13 '11 at 11:38
0

Yes, that is a bug and it will never be fixed. As a workaround, instead of checking for the availability of addChildViewController: method, you can check for removeFromParentViewController method. The latter is not available before iOS 5.0.

murat
  • 4,893
  • 4
  • 31
  • 29
0

It's quite possible this method existed in previous version of iOS, but it just wasn't public yet. Apple usually prepends private methods with an underscore but it has been known to do this kind of thing before.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151