简体   繁体   English

切换根视图控制器

[英]Switching root view controller

I've an app with UINavigationController already, but i want to switch to UITabBarController, the problem is when i switch to UItab from beginning it doesn't work, so i'm switching it in a delegate method but it doesn't work either! 我已经有一个带有UINavigationController的应用程序,但是我想切换到UITabBarController,问题是当我从一开始切换到UItab时它不起作用,所以我在委托方法中切换了它,但是它也不起作用! all code in the app delegate . 应用程序委托中的所有代码

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

The delegate method i want to switch on it: 我想打开它的委托方法:

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

Remember to handle the view transition: 记住要处理视图转换:

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

and you don't need makeKeyAndVisible after the first time. 而且您在第一次之后就不需要makeKeyAndVisible了。

You called - (void)presentWallViewControllerAnimated:(BOOL)animated but in the end of didFinishLaunchingWithOptions you called 您调用了- (void)presentWallViewControllerAnimated:(BOOL)animated但在您调用的didFinishLaunchingWithOptions结尾时

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

So the tab bar will never works ! 因此,标签栏将永远无法工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM