xcode 动态更改导航控制器的 rootviewcontroller
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10897489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing rootviewcontroller for navigation controller dynamically
提问by Sangeeta
I'm trying to change RootViewController
for NavigationController
in didFinishLaunchingWithOptions
.
But I don't know how can I do that.
我正在尝试更改RootViewController
为NavigationController
in didFinishLaunchingWithOptions
。
但我不知道我该怎么做。
I have gone through this link as well:
http://starterstep.wordpress.com/2009/03/05/changing-a-uinavigationcontroller%E2%80%99s-root-view-controller/
我也浏览过这个链接:http:
//starterstep.wordpress.com/2009/03/05/changed-a-uinavigationcontroller%E2%80%99s-root-view-controller/
Here is my code in didFinishLaunchingWithOptions
:
这是我的代码didFinishLaunchingWithOptions
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *rootController=[[HomePageController alloc] initWithNibName:@"HomePageController" bundle:nil];
navigationController=[[UINavigationController alloc] initWithRootViewController:rootController];
// presentation=[[PresentationController alloc]initWithNibName:@"PresentationController" bundle:nil];
//
// navigationController=[[UINavigationController alloc]initWithRootViewController:presentation];
//
// presentationList=[[PresentationListController alloc]initWithNibName:@"PresentationListController" bundle:nil];
//
// UINavigationController *listnavigation = [[UINavigationController alloc] initWithRootViewController:presentationList];
//
// revealer=[[ZUUIRevealController alloc]initWithFrontViewController:navigationController rearViewController:listnavigation];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Right now I comment and then run application to change rootviewcontroller
. However this is not the practical approach.
现在我评论然后运行应用程序来更改rootviewcontroller
. 然而,这不是实用的方法。
Any help will be appreciated.
任何帮助将不胜感激。
回答by Rui Peres
Instead of this:
取而代之的是:
[self.window addSubview:navigationController.view];
Put this:
把这个:
self.window.rootViewController = navigationController;
回答by sc0rp10n
A navigation controller does not care what type of view controller is its root view controller, as long as it is a subclass of UIViewController. So you can just use a pointer to a UIViewController like so:
导航控制器并不关心它的根视图控制器是什么类型的视图控制器,只要它是 UIViewController 的子类。所以你可以像这样使用指向 UIViewController 的指针:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootController = nil;
if (iWantHomePageController)
{
rootController = [[HomePageController alloc] initWithNibName:@"HomePageController" bundle:nil];
}
else if (iWantPresentationController)
{
rootController = [[PresentationController alloc] initWithNibName:@"PresentationController" bundle:nil];
}
else if (iWantPresentationListController)
{
rootController = [[PresentationListController alloc] initWithNibName:@"PresentationListController" bundle:nil];
}
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
回答by Kimpoy
This works really just fine for me:
这对我来说真的很好用:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
/*
both *navigationController and *viewController are declared
as properties in the .h file
*/
[self setViewController:[[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease]];
[self setNavigationController:[[[UINavigationController alloc] initWithRootViewController:self.viewController]autorelease]];
[self.window setRootViewController:[self navigationController]];
[self.window makeKeyAndVisible];