UIViewController says it responds to addChildViewController: on iOS 4

Has anyone else come across this? The following code says β€œYES” when running on iOS 4, but according to Apple's docs, the addChildViewController method is only available on iOS 5 and later. This does not seem to be the correct behavior, is it a mistake?

if([UIViewController instancesRespondToSelector:@selector(addChildViewController:)]) { NSLog(@"YES"); } else { NSLog(@"NO"); } 
+7
source share
3 answers

I think this is a mistake. The addChildViewController call seems to start without any warnings or errors.

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 iOS 4.3 Simulator, I see the following output. Both posts are limited to iOS 5.0 and above. It seems that addChildViewController is not responding correctly to the 4.3 simulator. I do not have a 4.3 device for testing on the device itself.

 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 will answer that this is the 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 am using Xcode 4.2 on Lion. When I look at UIViewController.h on the 4.3 Simulator platform, addChildViewController is not mentioned : or automatically ForwardAppearanceAndRotationMethodsToChildViewControllers, but the only SDK included is 5.0.

enter image description here

I believe that if you want to be careful, you can test the current version of iOS on a running device. See How to check iOS version?

+3
source

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

0
source

It is possible that this method existed in a previous version of iOS, but it was not yet publicly available. Apple usually prefers private methods with underlining, but, as you know, it did before.

0
source

All Articles