gpt4 book ai didi

ios - 以编程方式将 UIBarButtonItem 添加到 UINavigationBar

转载 作者:IT王子 更新时间:2023-10-29 08:08:18 32 4
gpt4 key购买 nike

我在 UIInterfaceBuilder 中放入了一个 UINavigationBar。我以模态方式呈现此 View ,只希望 UIBackBarButton 返回到我的上一个 View 。我声明了此 UINavigationBar 的导出和属性。我想在我的 viewDidLoad 方法中,我可以像这样创建一个 UIBackButton:

UIBarButtonItem *backButton = 
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
target:self
action:@selector(goBack)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];

但我在 UINavigationBar 上看不到我的 UIBackBarButtonItem。我想我在这里做错了什么,因为我不认为我的 UINavigationBar 知道我正在尝试以这种方式将此 UIBackBarButtonItem 添加到它。我是否必须创建一个 NSArray,将按钮放入其中,然后为 NavigationBar 设置 Items?

我也对 navigationItem 属性与 UINavigationBar 的 setItems 的工作方式感到困惑。任何帮助,将不胜感激。谢谢!

最佳答案

您正在尝试在不添加 backBarButtonItem 的模态视图中设置后退按钮项。这就是导致 Button(或任何类型的后退按钮)不显示的原因。 backBarButtonItem 主要与推送 View Controller 一起使用,当您推送新 View Controller (顶部项目)时,推送 View Controller 具有从父级(下面的下一项)添加的后退按钮。 Apple UINavigationItem Documentation说:

When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.

要像你希望的那样在左侧获得后退按钮,请尝试更改

self.navigationItem.backBarButtonItem = backButton;

self.navigationItem.leftBarButtonItem = backButton;

关于ios - 以编程方式将 UIBarButtonItem 添加到 UINavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6721169/

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