How to set parentViewController in UIViewController?

mrueg picture mrueg · Sep 7, 2009 · Viewed 16.1k times · Source

The parentViewController property of UIViewController is readonly, but I am nesting custom view controllers and would like to use this property.

However, since it is readonly, and I found no other way to set this property, my quesion is: how do I set it?

Obviously, UINavigationController can set the property somehow in -pushViewController, and so can -presentModalViewController, so it must be possible.

I am aware that I can just add my own UIViewController property, but I'm sure that parentViewController is, in principle, the correct property.

Answer

abuharsky picture abuharsky · May 10, 2010

Solution is:

   - (void)setParentController:(UIViewController*)parent{
 [self setValue:parent forKey:@"_parentViewController"];
    }

It does not cause problems with linker!

PS: Don't use "setParentViewController" as method name, because this method exists in private API and Apple say: "The non-public API that is included in your application is setParentViewController:.

If you have defined a method in your source code with the same name as the above mentioned API, we suggest altering your method name so that it no longer collides with Apple's private API to avoid your application being flagged with future submissions.

Please resolve this issue in your next update..."