目标c-PresentModalViewController未显示TabBar



我正在尝试从一个xib转到另一个,我正在使用TabBar。当我使用PresentModalViewController从xib移动到xib时,我会丢失TabBar。

当我使用这种方式时,它会失败(就像android中的强制关闭一样):

FirstViewController *fvc = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
    [fvc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    fvc.userSelectedLatitude = saveLatitude;
    fvc.userSelectedLongitude = saveLongtitude;
    UITabBarController *tabControl = [[UITabBarController alloc] initWithNibName:fvc bundle:nil];
    [self presentModalViewController:tabControl animated:NO];

当我使用:

UITabBarController *tabControl = [[UITabBarController alloc] initWithNibName:@"FirstViewController" bundle:nil];

我用TabBar获得黑屏。

既然一切都失败了,我想这不是正确的方式。那么,正确的方法是什么呢?

上面的代码崩溃,因为您试图在initWithNibName:bundle:方法中传递视图控制器而不是NSString对象。

方法取决于你真正想做什么。你想在带有或不带有tabBar的modalViewController中呈现xib吗?,或者简单地以模式呈现视图控制器?。

更新:

好吧,你必须首先创建与每个tabBar按钮关联的视图控制器(就像你到现在为止一直在做的那样),然后将这些视图控制器添加到你的tabBar中,然后以模式显示tabBarController。像这样:

FirstViewController *fvc = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
fvc.userSelectedLatitude = saveLatitude;
fvc.userSelectedLongitude = saveLongtitude;
UITabBarController *tabControl = [[UITabBarController alloc] init];
[tabControl setViewControllers:[NSArray arrayWithObjects:fvc, nil]];
[tabControl setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:tabControl animated:NO];

我认为这个代码应该有效。所以,试着告诉我们是否出了问题。

使用UITabBarController,无需手动呈现视图控制器或调用代码来切换视图。这是为您处理的。

您所需要做的就是设置UITabBarController的viewControllers属性。像这样:

[tabBarController setViewControllers:[NSArray arrayWithObjects:view1, view2, nil]];

最新更新