changing rootviewcontroller of a UINavigationController which is in storyboard while its initialised

▼魔方 西西 提交于 2019-12-12 01:25:41

问题


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:



来源:https://stackoverflow.com/questions/32944909/changing-rootviewcontroller-of-a-uinavigationcontroller-which-is-in-storyboard-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!