切换根视图控制器

时间:2016-06-29 01:11:46

标签: ios objective-c uiviewcontroller uinavigationcontroller uitabbarcontroller

我已经有了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];
}

2 个答案:

答案 0 :(得分:0)

请记住处理视图转换:

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

答案 1 :(得分:0)

您致电- (void)presentWallViewControllerAnimated:(BOOL)animated,但在didFinishLaunchingWithOptions结束时,您致电

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

所以标签栏永远不会有效!