Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController reported as responding to addChildViewController: on iOS 4

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");
}
like image 338
Hua-Ying Avatar asked Nov 15 '11 20:11

Hua-Ying


1 Answers

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?

like image 167
Joel Avatar answered Nov 03 '22 19:11

Joel