Interface Builder 中的 UITabBarControllers 遇到问题

发布于 2024-08-27 02:39:56 字数 1809 浏览 5 评论 0原文

我正在构建一个应用程序,其中根视图/窗口是基于选项卡的视图(使用用于创建基于选项卡的 iPhone 应用程序的 XCode 向导创建),但应用程序中还有一个点我想在其中创建另一个基于选项卡的视图并以模态方式呈现。

我在 IB 中创建基于模式选项卡的视图时遇到了很多麻烦,最终我只是在代码中完成了它,有点像这样:

// *** In the event handler that causes the second-tab view to be presented ***
MyTabViewController *tabVC = [[MyTabViewController alloc] init];
[self presentModalViewController:tabVC.tabBarController animated:YES];
[tabVC release];

// *** Inside init() definition in MyTabViewController.m ***

UIViewController *vc1 = [[MyViewController1 alloc] init];
UIViewController *vc2 = [[MyViewController2 alloc] init];

tabBarController_ = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController_.viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController_.selectedIndex = 0;

这工作正常,直到我开始尝试写入 tabBarController_.tabBar.items 来设置标题和图像按钮,它显然不想让您为 TabBarController 拥有的 TabBar 执行此操作,从而给出此错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 
'Directly modifying a tab bar managed by a tab bar controller is not allowed.'

所以我尝试返回使用 Interface Builder 实现 MyTabViewController ,以便我可以在那里设置按钮,但我不明白。这些是我为实现目标所采取的步骤:

  1. 在 XCode 中创建从 UIViewController 继承的新类,并选中“with XIB”选项。
  2. 将 TabBarController 拖到 XIB 中(后面有黄色球的那个)。

我不明白的是如何让 TabBar 接管视图。现在,XIB 附带的视图自动为空,并且我的 UITabBarController 与它完全断开连接。如果我尝试将 TabBar 从 UITabBarController 中拖动到视图中,它似乎会创建一个新的 TabBar,而不是将我的 TabBar 放置到视图中以使其占据整个视图。

如果我没有非常清楚地解释这一点,我很抱歉,但我真的很难理解 TabBarController 和 View 之间的链接,即无法弄清楚如何让 TabBarController 实际显示。

任何对此的帮助将不胜感激。如果有帮助的话,我附上了 IB 的屏幕截图。

替代文本 http://www.shelltoesmusic.com/files/tabbar_ib.png

I'm building an app in which the root view/window is a tab-based view (created using the XCode wizard for creating a tab-based iPhone app), but there is also a point in the app where I want to create another tab-based view and present it modally.

I was having so much trouble creating the modal tab-based view in IB that I eventually just did it in code, kinda like this:

// *** In the event handler that causes the second-tab view to be presented ***
MyTabViewController *tabVC = [[MyTabViewController alloc] init];
[self presentModalViewController:tabVC.tabBarController animated:YES];
[tabVC release];

// *** Inside init() definition in MyTabViewController.m ***

UIViewController *vc1 = [[MyViewController1 alloc] init];
UIViewController *vc2 = [[MyViewController2 alloc] init];

tabBarController_ = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController_.viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController_.selectedIndex = 0;

This worked fine until I started trying to write to tabBarController_.tabBar.items to set the titles and images for the buttons, which it apparently doesn't want to let you do for a TabBar that is owned by a TabBarController, giving this error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 
'Directly modifying a tab bar managed by a tab bar controller is not allowed.'

So I tried going back to implement MyTabViewController using Interface Builder so that I could set the buttons up in there, but I can't figure it out. These are the steps I took to get where I am:

  1. Created new class descending from UIViewController in XCode, and checked the "with XIB" option.
  2. Dragged a TabBarController into the XIB (the one with the yellow ball behind it).

The thing I can't figure out is how to get the TabBar to take over the view. Right now the View that comes with the XIB automatically is empty, and I have this UITabBarController that is totally disconnected from it. If I try to drag the TabBar from within the UITabBarController into the View, it appears to make a new TabBar instead of positioning my TabBar into the view so that it occupies the whole view.

I apologise if I haven't explained this very clearly, but I'm really struggling to understand the linkage between the TabBarController and the View, i.e. can't figure out how to get the TabBarController to actually display.

Any help with this would be much appreciated. I have attached a screengrab from IB if that helps at all.

alt text http://www.shelltoesmusic.com/files/tabbar_ib.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴者 2024-09-03 02:39:56

选项卡栏控制器从它管理的视图控制器中获取选项卡的标签和图像。标签来自视图控制器的 title。该图像来自视图控制器的 tabBarItem 属性。您可以在 MyViewController1 和 MyViewController2 的 init 方法中设置这两者。

- (id)init {
    if (self = [super initWithNibName:@"MyViewController" bundle:nil]) {
        self.title = @"My View Controller";

        UIImage* anImage = [UIImage imageNamed:@"MyViewControllerImage.png"];
        UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:anImage tag:0];
        self.tabBarItem = theItem;
        [theItem release];
    }
    return self;
}

每个视图控制器都有一个 view 属性。标签栏控制器也是一个视图控制器,因此它也有一个 view 属性。将选项卡栏控制器的 view 属性添加到当前空白的视图中。

示例:

[view addSubview:tabBarController.view];

请参阅 文档(如果您有疑问)。非常完整。

The tab bar controller picks up the labels and images for the tabs from the view controllers it manages. The label comes from the view controllers' title. The image comes from the view controllers' tabBarItem properties. You can set both of these up in the init method of MyViewController1 and MyViewController2.

- (id)init {
    if (self = [super initWithNibName:@"MyViewController" bundle:nil]) {
        self.title = @"My View Controller";

        UIImage* anImage = [UIImage imageNamed:@"MyViewControllerImage.png"];
        UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:anImage tag:0];
        self.tabBarItem = theItem;
        [theItem release];
    }
    return self;
}

Every view controller has a view property. The tab bar controller is also a view controller so it too has a view property. Add the tab bar controller's view property to the view that is currently blank.

Example:

[view addSubview:tabBarController.view];

See documentation if you have questions. It's very complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文