简体   繁体   中英

Difference between Inheriting the UINavigationController class and assigning the rootViewController

What is the difference between inheriting the UINavigationController class and assigning the rootViewController property (or using initWithRootViewController: method)??

I am bit confuse about this. Consider below code inheritance of UINavigationController :

@interface NativeViewController : UINavigationController
{

}

Now consider below code using initWithRootViewController: method:

UINavigationController *viewController = nil;
if (self) {
    NativeViewController *vc = [[NativeViewController alloc] initWithNibName:@"NativeViewController" bundle:nil];
    viewController = [[UINavigationController alloc] initWithRootViewController:vc];
    [vc autorelease];
}
return viewController;

When to use Inheritance like the first case and when to use initWithRootViewController: Method??

What will happen to rootViewController property in case of inheritance??

Same question is for UITabbarController class.

First one is for customizing UINavigationController . In other hands, second one is for initializing the UINavigationController with very first viewcontroller in it. Let say, UINavigationController itself is just placeholder for navigating UIViewController s.

You still should put rootViewController in case of inheritance of UINavigationController .

Subclass the container (UINavigationController, UITabBarController) only in situations where you need control over the container's functionality or appearance that isn't provided by UIAppearance or delegate methods.

In all other cases (which will be the majority), you'd set the root view controller or viewControllers properties. You don't need to subclass to provide basic functionality. These are containers - most of your UI comes from the child view controllers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM