开发者

What is the difference between the two statements below?

开发者 https://www.devze.com 2023-04-12 19:23 出处:网络
self.window.rootViewController = self.tabBarController; [self.window addSubview:self.开发者_开发问答tabBarController.view];
self.window.rootViewController = self.tabBarController;
 [self.window addSubview:self.开发者_开发问答tabBarController.view];

They are used in the context below:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    // Add the tab bar controller's current view as a subview of the window


    //   self.window.rootViewController = self.tabBarController;
     [self.window addSubview:self.tabBarController.view];

    IntroViewController *introViewController = [[IntroViewController alloc] initWithNibName:@"IntroViewController" bundle:nil];

    //Lets place introViewController in navController
    UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:introViewController];

    //Now lets display it 
    [self.tabBarController presentModalViewController:navController animated:YES];

    [navController release];
    [introViewController release];


    [self.window makeKeyAndVisible];
    return YES;
}


From iOS Reference:

rootViewController

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.

addSubview

This method retains view and sets its next responder to the receiver, which is its new superview.

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

So we can say that the main difference is that setting rootViewController destroys all the previous views contained in the UIWindow, and using addSubView: only adds an UIView on top.


self.window.rootViewController = self.tabBarController;

This statement is wrong because window is a container you don't have any root controller there.

SubView: [self.window addSubview:self.tabBarController.view];

Here you are adding the tabBarController as a subview which will add your windows container. And this is the right way to create the tab bar controller.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号