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");
}

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.

在这里输入图像描述

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?


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.


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.

链接地址: http://www.djcxy.com/p/10008.html

上一篇: 一种监视控件的屏幕位置何时更改的方法?

下一篇: UIViewController报告为响应addChildViewController:在iOS 4上