gpt4 book ai didi

ios - 显示 tabbarcontroller View 时将 RightButtonItem 添加到导航栏

转载 作者:行者123 更新时间:2023-12-01 18:59:06 26 4
gpt4 key购买 nike

在我的应用程序中,我有 tabbarcontroller那不是第一个viewcontroller .它将以 navigationcontroller 的形式打开.(通过将其从其父级插入堆栈)

现在我想在这个 View 的导航栏中添加一个 rightbarbuttonitem。我正在使用这段代码:

UIBarButtonItem *b= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionBarButtonPressed)];
self.navigationController.navigationItem.rightBarButtonItem= b;

但它不起作用 - 导航栏中没有出现任何按钮。我对此进行了搜索,但在所有主题中,此代码已作为工作解决方案得到解答。使用 tabbar 时可能会有所不同和 navigationbar一起。有谁知道任何其他可行的解决方案。

编辑
我在最后一行添加了一个断点广告并检查了一些变量。这是结果:
(lldb) po self.navigationController.navigationItem.rightBarButtonItem
error: property 'navigationItem' not found on object of type 'id'
error: 1 errors parsing expression
(lldb) po self.navigationController;
nil
(lldb) po self.navigationItem;
<UINavigationItem: 0x8997070>
(lldb)
(lldb) po self.navigationController.navigationItem.rightBarButtonItem
error: property 'navigationItem' not found on object of type 'id'
error: 1 errors parsing expression

看来 navigationControllernil我们应该直接调用 navigationItem .所以我更改了代码,但再次无法正常工作。所以我检查了这些变量:
(lldb) po self.navigationItem
<UINavigationItem: 0x8d789c0>
(lldb) po self.navigationItem.rightBarButtonItem
error: property 'rightBarButtonItem' not found on object of type 'id'
error: 1 errors parsing expression
(lldb)

似乎 rightbarbuttonitem不存在。那么我们现在应该怎么做呢?

最佳答案

您无法更改右栏按钮项目的原因是因为只有您的标签栏 Controller 位于导航 Controller 堆栈中。 View Controller 只能访问标签栏 Controller 。

因此,为了拥有自定义导航栏按钮,您必须在 UITabBarController 中添加/设置它。 .

如果你想拥有不一样的UIBarButtons对于每个 View Controller ,都有委托(delegate)方法 tabBarController:didSelectViewController:您可以在其中根据所选 View Controller 更改 UIBarButtonItem。

关于ios - 显示 tabbarcontroller View 时将 RightButtonItem 添加到导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24096785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com