-3

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?

Daizy
  • 331
  • 2
  • 5
  • 12

2 Answers2

0

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.

Greg
  • 25,317
  • 6
  • 53
  • 62
  • I think it's different in implementation. Set root view controller is make the window view become a reference to the root view vontroller's view. While addSubview is to set a subview – Daizy Nov 06 '14 at 10:12
  • exactly, I want to know the meaning(implementation) of the "content view". using 'addSubview' is also a way to get a "content view" – Daizy Nov 06 '14 at 10:28
  • @youKnowDai addSubview adds a view to the end of the list of subviews and that's it. But rootViewController assign the view controller to it's property, add view controller's view as the content view of the window and remove views from the hierarchy if already exists. – Greg Nov 06 '14 at 10:38
  • I may Got it. Do You mean: window and rootviewcontroller's relationship is similar with the relationship between normal view and viewController.@Greg – Daizy Nov 06 '14 at 15:38
  • I really made a bad question... sorry :( @Greg – Daizy Nov 06 '14 at 15:48
-2

Obviously not. The root view controller is generally assigned to window in appdelegate class. Also, root view controller is always associated with UINavigationController. So that any root view controller of a UINavigationController will be a its content view controller.

Where as, add subview is just a method of UIView class. Which helps to add any subview to the respective view.

R_Developer
  • 864
  • 1
  • 7
  • 10
  • 2
    @R_Developer, please correct this line, `Also, root view controller is always associated with UINavigationController`.. its not always, but may be and depend on requirements. – Hemang Nov 06 '14 at 10:29
  • @Hemang, I didn't get you. If you can explain this more. May be that could add some more in my knowledge. – R_Developer Nov 06 '14 at 10:32
  • 2
    @R_Developer, Yes sure, a `rootViewController` can be a navigation controller or `tabBarController` or a normal `UIViewController`. – Hemang Nov 06 '14 at 10:47