如何呈现TabBarController模式

时间:2013-12-26 18:44:18

标签: ios viewcontroller tabbar

从一个视图(LoginTesteInicial)我可以转到两个tabBarControllers,但是当我运行代码时,它崩溃了这个错误: Attempt to present <UITabBarController: 0x8a4a870> on <LoginTesteInicial: 0x8a46970> whose view is not in the window hierarchy!

这是我LoginTesteInicial.m的代码:

UITabBarController *vc;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

    vc = [[UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil] instantiateViewControllerWithIdentifier:@"TabBarSemLogin"];
} else {

    vc = [[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"TabBarSemLogin"];
}

[vc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

[self presentViewController:vc animated:YES completion:nil];

2 个答案:

答案 0 :(得分:3)

您的问题的答案是,当调用-viewDidLoad时,视图控制器的视图不在视图层次结构中。您需要等到视图放入视图层次结构中。这可以在-viewWillAppear:-viewDidAppear:中完成。


“获得开始/结束外观转换的不平衡调用”警告是因为视图控制器在被另一个视图控制器替换之前尚未完全加载。要避免该警告,可以使用-performSelector:withObject:afterDelay:在下一个运行循环中安排当前视图控制器。

- (void)viewDidAppear:(BOOL)animated
{
    …
    [self performSelector:@selector(showTabBarController) withObject:nil afterDelay:0.0];
}

- (void)showTabBarController
{
    UITabBarController *vc;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        vc = [[UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil] instantiateViewControllerWithIdentifier:@"TabBarSemLogin"];
    } else {
        vc = [[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"TabBarSemLogin"];
    }

    [vc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:vc animated:YES completion:nil];
}

答案 1 :(得分:1)

移动[self presentViewController:vc animated:YES completion:nil]及其相关代码

1 viewWillAppear

2 viewWillLayoutSubviews

3 viewDidLayoutSubviews

您需要保留一个标记,即使其中一个方法触发多次,您的视图也只会显示一次。

或者您可以避免呈现视图并添加为子视图,如果您不喜欢保留标记等,请执行此操作而不是呈现。

[self.view addSubview:vc.view];

[self addChildViewController:vc];

[vc didMoveToParentViewController:self] 
相关问题