changing rootviewcontroller of a UINavigationContr

2019-09-12 22:33发布

问题:

I have a UINavigationController controller in the story board which is the entry point,whose class is MyNavigationController. I have not assigned the rootviewcontroller to the UINavigationController, from the story board ,but want to assign the same from MyNavigationController,From some initialize methods Please help me how to do that or is it impossible?.

回答1:

choice-1

you can Identify your Stroryboard ID and set as Root

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginSignupVC"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
}

Choice-2

the Initial View COntroller automatically identify your Root controller

you can Identify your Stroryboard ID on your viewcontroller using Attribute

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyNavigationController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginSignupVC"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
}

Choice-3

xcode --> Menu -> editor -> embedIn -> NavigationController

you can get like following output

it find automatically your root controller



回答2:

So you subclassed UINavigationController, if I understand correctly and you want it to be initialised with a given view controller.

You can override init in MyNavigationController and call [super initWithRootViewController:myRootViewController], where myRootViewController is an instance of a UIViewController or its subclass.

You would then use MyNavigationController as such:

MyNavigationController *myNavigationController = [[MyNavigationController alloc] init];

For more info, see: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/initWithRootViewController: