0

我正在尝试更改in RootViewController。 但我不知道我该怎么做。NavigationControllerdidFinishLaunchingWithOptions

我也浏览了这个链接:
http ://starterstep.wordpress.com/2009/03/05/changeing-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. 然而,这不是实用的方法。

任何帮助将不胜感激。

4

3 回答 3

5

而不是这个:

[self.window addSubview:navigationController.view];

把这个:

self.window.rootViewController = navigationController;
于 2012-06-05T12:42:27.430 回答
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;
}
于 2012-06-05T16:06:53.060 回答
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];
于 2012-06-06T00:38:30.477 回答