What does setting the UIWindow's rootViewController do?

后端 未结 1 1204
谎友^
谎友^ 2020-12-28 07:59

Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view

相关标签:
1条回答
  • 2020-12-28 08:46

    Before the rootViewController property came along, most apps had code like this in the application delegate:

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

    This code set the view controller's view as the main view, but the UIWindow instance had no reference to the controller owning that view.

    When you use the rootViewController property, you don't need to add the view controller's view to the UIWindow instance anymore, this is done automatically. So the number of lines of code stays the same, but now your UIWindow has a reference to the view controller.

    So, in newer applications, we now have code that looks like this:

    window.rootViewController = viewController;
    [window makeKeyAndVisible];
    
    0 讨论(0)
提交回复
热议问题