Search code examples
iosobjective-cuiviewuiwindow

rootViewController and addSubview in Objective-C?


Im starting to learn OC.

The first question is about _window.rootviewcontroller and [_window addSubview:...]

Both of the two ways can set view for UIWindow (actually, UIWindow is inherited from UIView).

So what I want to know is :

Is setting the rootviewcontroller for window just using the addSubview method to implement , or it's something else?

more exactly:

is

_window.rootviewcontroller = viewController;

meaning

[_window addSubview: viewController.view];

or

_window = viewController.view; //UIWindow : UIView

or something else?

Thanks a lot.

Is there anyone who can tell me some details about UIWindow and the rootViewController property?


Solution

  • If you use addSubview: to have to pass a UIView instance but when you call rootviewcontroller you passing UIViewController instance to the UIWindow.

    You can use addSubview but you have to associate UIView superview (Which needs to be UIViewController) to the UIWindow, to make it behave the same, something like that (old way to to that:

    [window addSubview:myViewController.view];
    [window makeKeyAndVisible];
    

    By using rootviewcontroller it will do it for you.

    This is taken from Apple:

    The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.