Brad Thomas Brad Thomas - 1 year ago 166
iOS Question

Presenting view controllers on detached view controllers is discouraged - definition of "detached view controller"?

OK. So I've looked around the web and read similar questions about this relatively new iOS warning. My app uses NO storyboard. I only have one simple question. What is a "detached view controller" and can anyone provide a definitive reference (e.g. to an Apple doc) that provides a definition of "detached view controller" as we are to understand it in the context of this warning. I fail to see how other answers to this question are more than fumbles and guesses around the topic if no-one truly understands precisely what a detached view controller is.

Answer Source

A detached view controller is one that is not currently in the hierarchy of [[[UIApplication sharedApplication] keyWindow] rootViewController]

For example, I could instantiate a UIViewController anywhere in my code, but if I never put it in this hierarchy by making it my rootViewController or pushing onto a navigation stack in my rootViewController, then I would get strange/undefined behavior when I present something on it.


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIViewController* a = [[UIViewController alloc] init];
    UIViewController* b = [[UIViewController alloc] init];
    UIViewController* someOtherVC = [[UITableViewController alloc] init];

    self.window.rootViewController = a;
    //Works fine :)
    [a presentViewController:someOtherVC animated:YES completion:nil];

    //might break the world
    [b presentViewController:someOtherVC animated:YES completion:nil]; 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download