dynamic更改导航控制器的rootview控制器
我正在尝试在didFinishLaunchingWithOptions
更改NavigationController
RootViewController
。
但我不知道我该怎么做。
我也经历了这个链接:
http://starterstep.wordpress.com/2009/03/05/changing-a-uinavigationcontroller%E2%80%99s-root-view-controller/
这是我在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; }
现在我评论,然后运行应用程序来更改rootviewcontroller
。 然而这不是实际的方法。
任何帮助将不胜感激。
而不是这个:
[self.window addSubview:navigationController.view];
把这个:
self.window.rootViewController = navigationController;
只要它是UIViewController的子类,导航控制器就不关心它的根视图控制器是什么types的视图控制器。 所以你可以像这样使用一个指向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; }
这对我来说真的很好:
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];