gpt4 book ai didi

iOS 检查后退按钮是否显示在 UINavigationController 中

转载 作者:行者123 更新时间:2023-12-01 17:22:34 24 4
gpt4 key购买 nike

在 iOS 项目中,我需要检查当前是否显示后退按钮。我尝试了 SO 上提供的一些解决方案,但没有一个对我有用。目前我正在使用这段代码

NSArray *stack = navigationController.viewControllers;
int i = stack.count-2;

if (i>=0)
{
UIViewController *backVC = (UIViewController*)[stack objectAtIndex:i];
if (backVC.navigationItem.backBarButtonItem != nil) {
NSLog(@"Back button is displayed!");
}
}

但是日志中什么也没有出现。如果我正确理解了苹果指南,我正在寻找位于索引 n-2 处的堆栈中的 ViewController,该 ViewController 应该按住后退按钮。

我在里面使用这段代码-navigationController:willShowViewController:animated:

最佳答案

我今天遇到了同样的问题,在尝试了上述代码的变体之后,我决定使用以下方法检测是否应显示后退按钮:

 self.navigationController.navigationBar.backItem

来自文档:

If the leftBarButtonItem property of the topmost navigation item is nil, the navigation bar displays a back button whose title is derived from the item in this property. If there is only one item on the navigation bar’s stack, the value of this property is nil..

这满足了我的需求。希望对您有所帮助,否则我很想听听您是如何解决的!

关于iOS 检查后退按钮是否显示在 UINavigationController 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26466842/

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