动态更改导航控制器的rootviewcontroller

时间:2012-06-05 12:40:29

标签: iphone ios xcode

我正在尝试更改RootViewControllerNavigationController的{​​{1}} 但我不知道我该怎么做。

我也经历过这个链接:
http://starterstep.wordpress.com/2009/03/05/changing-a-uinavigationcontroller%E2%80%99s-root-view-controller/

以下是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; } 。然而,这不是实用的方法。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:4)

而不是:

[self.window addSubview:navigationController.view];

把这个:

self.window.rootViewController = navigationController;

答案 1 :(得分:1)

导航控制器不关心它的根视图控制器是什么类型的视图控制器,只要它是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;
}

答案 2 :(得分:0)

这对我来说真的很好:

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];