简体   繁体   中英

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! 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.

You called - (void)presentWallViewControllerAnimated:(BOOL)animated but in the end of didFinishLaunchingWithOptions you called

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

So the tab bar will never works !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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