【问题标题】:Switching root view controller切换根视图控制器
【发布时间】:2016-06-29 01:11:46
【问题描述】:

我已经有一个带有 UINavigationController 的应用程序,但我想切换到 UITabBarController,问题是当我从一开始切换到 UItab 时它不起作用,所以我在委托方法中切换它但它没有也不行! 应用委托中的所有代码

self.navigationController = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]];
    self.tabBarController = [[UITabBarController alloc] init];


    if ([PFUser currentUser]) {
        // Present wall straight-away
        [self presentWallViewControllerAnimated:NO];
    } else {
        // Go to the welcome screen and have them log in or create an account.
        [self presentLoginViewController];
    }

    [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

我要开启的委托方法:

- (void)presentWallViewControllerAnimated:(BOOL)animated {
    NSLog(@"Called:presentWallViewControllerAnimated ");
//    self.navigationController = nil;

    self.tabBarController = [[UITabBarController alloc] init];

    PAWWallViewController *wallViewController = [[PAWWallViewController alloc] initWithNibName:nil bundle:nil];
    wallViewController.delegate = self;


    // Set up the first View Controller
    UIViewController *vc1 = [[UIViewController alloc] init];
    vc1.view.backgroundColor = [UIColor orangeColor];
    vc1.tabBarItem.title = @"Orange";
    vc1.tabBarItem.image = [UIImage imageNamed:@"heart"];

    // Set up the second View Controller
    UIViewController *vc2 = [[UIViewController alloc] init];
    vc2.view.backgroundColor = [UIColor purpleColor];
    vc2.tabBarItem.title = @"Purple";
    vc2.tabBarItem.image = [UIImage imageNamed:@"star"];

    // Set up the Tab Bar Controller to have two tabs

    [self.tabBarController setViewControllers:@[ vc1, vc2]];

    self.window.rootViewController = self.tabBarController;

    [self.window makeKeyAndVisible];



//    [self.window addSubview:tabBarController.view];
//    [self.navigationController setViewControllers:@[ tabBarController ] animated:animated];
}

【问题讨论】:

    标签: ios objective-c uiviewcontroller uinavigationcontroller uitabbarcontroller


    【解决方案1】:

    记得处理视图转换:

    UIViewController *vc = // any vc that's initialized properly    
    window.rootViewController = vc;
    
    [UIView transitionWithView:window
                      duration:0.3  // 0.0 for immediate
                       options:UIViewAnimationOptionTransitionCrossDissolve // several enums to choose from here
                    animations:nil
                    completion:nil];
    

    第一次之后你就不需要makeKeyAndVisible了。

    【讨论】:

    • 这也不起作用,我仍然看到带有 blackview 的导航栏
    【解决方案2】:

    你打电话给- (void)presentWallViewControllerAnimated:(BOOL)animated,但在didFinishLaunchingWithOptions结尾你打电话

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    

    所以标签栏永远不会起作用!

    【讨论】:

    • 那我该怎么办?
    • 删除didFinishLaunchingWithOptions末尾的这些代码。并检查上面 if/else if ([PFUser currentUser]) 代码中的 rootViewController
    • 但我需要导航控制器来显示登录或注册视图?
    • 好的。我认为您可以将 rootViewController 设置为标签栏。如果您需要登录视图,您可以呈现不带动画的登录/注册视图。完成后,关闭它并返回标签栏。
    • 我得到“试图在 上显示 ,其视图不在窗口层次结构中!”
    猜你喜欢
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2017-12-02
    • 1970-01-01
    相关资源
    最近更新 更多