gpt4 book ai didi

ios - 如何找出属于当前 viewController 的导航 Controller ?

转载 作者:行者123 更新时间:2023-12-01 18:43:19 25 4
gpt4 key购买 nike

我有
在导航 Controller 上显示按钮时出现问题。我对此进行了研究,我认为这与导航的工作方式有关。从堆栈中推送和弹出。

这是我所拥有的:

我将初始 View 作为导航 Controller 。此 Controller 调用多个 View ,但我遇到问题的 View 是 UICollectionViewController。这是通过单击连接到 navigationController 的第一个 viewController 的按钮调用的。然后我有一个从 UICollectionViewCell 到新 ViewController 的 segue 设置。所以我假设这是一个二级 NavigationController。

其次,我有一个后退按钮图像显示在我的导航上,而不是文本 < 返回。

除了从第二级 segue 调用的 View 之外,所有 View 的导航 Controller 中的后退按钮都显示良好。

正如我从阅读有关此主题的其他帖子中了解到的那样,这是因为在第二级 segue 上创建了一个新的导航 Controller 。

我对此很好,因为一切正常,但我需要在第二个导航 Controller 上显示按钮。

我试图以编程方式将按钮添加到与当前 View 关联的导航 Controller 。我正在使用这段代码:

override func viewDidLoad() {
super.viewDidLoad()
var image = UIImage(named: "NavLogo")
var back_image = UIImage(named: "BackButton")
image = image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
back_image = back_image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)


self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: nil, action: nil)

self.navigationController?.navigationItem.leftBarButtonItem = UIBarButtonItem(image: back_image, style: UIBarButtonItemStyle.plain, target: nil, action: nil)
}

此代码似乎没有将按钮添加到当前导航 Controller 。

该项目使用 Swift 3 和 Xcode 8

最佳答案

采用

self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: nil, action: nil)

代替
self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: nil, action: nil)

原因在此 link 中进行了解释。 .

关于ios - 如何找出属于当前 viewController 的导航 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39498189/

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