【问题标题】:Changing rootviewcontroller for navigation controller dynamically动态更改导航控制器的 rootviewcontroller
【发布时间】:2012-06-05 12:40:29
【问题描述】:

我正在尝试将RootViewController 更改为NavigationController 中的didFinishLaunchingWithOptions
但我不知道该怎么做。

我也浏览过这个链接:
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。然而,这不是实际的方法。

我们将不胜感激。

【问题讨论】:

    标签: iphone ios xcode


    【解决方案1】:

    而不是这个:

    [self.window addSubview:navigationController.view];
    

    把这个:

    self.window.rootViewController = navigationController;
    

    【讨论】:

    • 你能详细解释一下吗?
    • 您所做的只是将您的navigationViewController 的视图添加到Window 的顶部...这不是您想要的。您将在不久的将来遇到问题,例如轮换。
    【解决方案2】:

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

    【讨论】:

    • 我可以从视图控制器访问这些变量:iWantHomePageController、iWantPresentationController 吗?如果是,那么我可以相应地设置值,一旦我设置了值,这个事件:'didFinish..' 会被触发吗?如果是,那么我完成了。所以请解释一下我可以这样做吗?如果是,那怎么办?
    • 这些变量只是我为了演示根据您选择的逻辑将根视图控制器更改为您想要的任何内容的能力而编造的。您说您想动态设置它们,因此您必须使用一些逻辑来决定根视图控制器应该是什么。因此,如果该逻辑确定您需要主页控制器,那么您将 rootController 设置为 HomePageController,其余部分相同。
    • 如果您希望能够从应用程序的其他位置设置这些值,您需要找到一种不同的方式来尝试做您想做的事情。 didFinishLaunchingWithOptions 仅在应用初始启动时调用,在应用打开时不会再次调用。
    【解决方案3】:

    这对我来说真的很好用:

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

    【讨论】:

    • 感谢您的回答..我希望动态更改 rootviewcontroller。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多