0

我有一个 UITabBarController 并想添加一个选项卡,使其能够消失。我用 XCode 添加了 2 个选项卡。您可以通过程序添加第三个选项卡吗?我知道这样的命令:

  [self setViewControllers: [NSArray arrayWithObjects: x1, nil] animated: NO];

您可以检索使用 XCode 添加第三个视图的数组吗?谢谢


我无法使用代码加载视图。我从情节提要创建了一个视图控制器,当我尝试从代码中加载它时出现黑屏,请使用以下代码:

ViewControllerA *x1 = [[ViewControllerA alloc] init];
[self setViewControllers:[NSArray arrayWithObjects: x1, nil] animated:NO];
4

2 回答 2

3

是的,如果您使用,[UITabViewController setViewControllers: animated:]您可以添加一个包含两个先前视图控制器和新的第三个视图控制器的数组。

例如,您可能希望这样做:

// assuming you've set an IBOutlet to your tab bar controller
NSArray * currentSetOfViewControllers = [appTabBarController viewControllers];
if((currentSetOfViewControllers == NULL) || [currentSetOfViewControllers count] == 0))
{
    NSLog( @"I don't see any view controllers; is my tab bar controller outlet set properly?")
} else {
    NSMutableArray newSetOfViewControllers = [[NSMutableArray alloc] initWithArray: currentSetOfViewControllers];
    if(newSetOfViewControllers)
    {
        ViewControllerA *x1 = [[ViewControllerA alloc] init];
        if(x1)
        {
            [newSetOfViewControllers addObject: x1];

            [appTabBarController setViewControllers: newSetOfViewControllers];

            // release our alloc'd mutable array only if you do not have ARC turned on
            [newSetOfViewControllers release];
        }
    }
}

您可能还想为您的新视图控制器提供一个带有标题和图像的关联标签栏项目。退房[UITabBarItem initWithTitle: image: tag:]

我已经为您链接了 Apple 文档,希望对您有所帮助!

于 2012-06-04T19:07:42.043 回答
0

在委托中创建一个自定义选项卡栏,因此创建一个类似于选项卡栏的视图,并在视图和 3 个按钮(1 个按钮设置为隐藏)上设置 1 个图像视图(1 个按钮设置为隐藏)在您需要第三个按钮启用第三个按钮之后在选项卡栏控制器中添加此视图

于 2012-06-05T10:37:18.287 回答