伊塔巴尔和伊塔巴里特姆

发布于 2024-11-18 01:27:32 字数 438 浏览 2 评论 0 原文

我正在尝试将 UITabBarItems 添加到 UITabBar 而不是选项卡栏控制器。这是我尝试做的。当我调用 setItems 时它总是崩溃。任何人都可以指出什么问题吗?

My_Accounts *my_AccountsVC = [[My_Accounts alloc] init];
Payments *paymentsVC = [[Payments alloc] init];
Transfer *transferVC = [[Transfer alloc] init];
NSArray *VCArray = [[NSArray alloc] initWithObjects:my_AccountsVC,paymentsVC,transferVC, nil];
[self.tabbar setItems:VCArray];

谢谢

I am trying to add UITabBarItems to a UITabBar not to a tabbar controller. Here is what I tried to do. It is always crashing when I am calling setItems. Can any please point out whats wrong.

My_Accounts *my_AccountsVC = [[My_Accounts alloc] init];
Payments *paymentsVC = [[Payments alloc] init];
Transfer *transferVC = [[Transfer alloc] init];
NSArray *VCArray = [[NSArray alloc] initWithObjects:my_AccountsVC,paymentsVC,transferVC, nil];
[self.tabbar setItems:VCArray];

Thanks

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

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

发布评论

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

评论(3

客…行舟 2024-11-25 01:27:32

如果您查看 items,它需要一个 UITabBarItem 数组,而不是您似乎正在传递的 UIViewController 子类。

您必须跟踪其他地方的视图控制器,并传递一个 UITabBarItem 数组,并在 UITabBar 的委托中处理视图控制器。

或者更好的是,使用 UITabBarController

If you look at items, it takes an array of UITabBarItems and not UIViewController subclasses which you seem to be passing.

You will have to keep track of the view controllers elsewhere and pass an array of UITabBarItems and handle the view controllers in the UITabBar's delegate.

Or much better, use UITabBarController.

遇到 2024-11-25 01:27:32

我相信您误解了 UITabBarController 的工作原理(文档链接)。您必须使用 viewControllers 属性将 UIViewController 添加到 UITabBarController

最后一行应为:

[tabBarController setViewControllers:VCArray];

UITabBarControllertabBar 属性是只读的。你不能这样设置。

如果您有一个 UITabBar (文档链接),没有 UITabBarViewController,那么您将需要使用以下方法:

- (void)setItems:(NSArray *)items animated:(BOOL)animated

但是,这些项目不是 UIViewController!它们是 UITabBarItem 的实例(文档链接)。您可以通过将它们放入数组中来一次性设置所有这些,也可以为每个视图控制器设置它们。您可以使用多个系统项目(更多、收藏夹等),或者您可以使用 – initWithTitle:image:tag: 创建自定义项目。

I believe you are misunderstanding how a UITabBarController works (documentation link). You must add the UIViewControllers to the UITabBarController using the viewControllers property.

The last line you have should read:

[tabBarController setViewControllers:VCArray];

The tabBar property of the UITabBarController is read-only. You cannot set that.

If you have a UITabBar (documentation link) without a UITabBarViewController, then you will need to use the method:

- (void)setItems:(NSArray *)items animated:(BOOL)animated

However, these items are not UIViewControllers! They are instances of UITabBarItem (documentation link). You may set these all at once by putting them into an array, or you can set them per view controller. There are several system items you may use (More, Favorites, etc) or you may use – initWithTitle:image:tag: to create a custom item.

旧情勿念 2024-11-25 01:27:32

代码好像有错。我想

[self.tabbar setItems:VCArray];

上面的行应该有 UITabBarItems 数组的参数。我猜你传递了 UIViewController 的项目。您应该创建 UITabbarItems 并在 setItems 方法中传递该数组。

你应该做如下的事情:

    UITabBarItem *tabOne = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
    UITabBarItem *tabTwo = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];

    NSArray *arrTabbarItems = [NSArray arrayWithObjects:tabOne,tabTwo, nil];

    [tabbar setItems:arrTabbarItems];

我不确定它会做什么,因为我总是使用 UITabBarController。希望这有帮助。

Code seems wrong. I guess

[self.tabbar setItems:VCArray];

Above line should have parameter of Array of UITabBarItems. You passed items of UIViewController I guess. You should Create UITabbarItems and pass array of that in setItems method.

You should do something like below:

    UITabBarItem *tabOne = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0];
    UITabBarItem *tabTwo = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];

    NSArray *arrTabbarItems = [NSArray arrayWithObjects:tabOne,tabTwo, nil];

    [tabbar setItems:arrTabbarItems];

I am not sure what it will do as I am always using UITabBarController. Hope this help.

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