[英]How do I find out the navigation controller that is part of the current viewController?
I'm having a problem getting a button to show on a navigation controller. 我在导航控制器上显示按钮时遇到问题。 I have researched this and I think it has to do with how navigation works.
我研究了这个,我认为它与导航的工作方式有关。 Pushed and Popped from a stack.
从堆栈中推出并弹出。
Here is what I have: 这是我有的:
I have the initial view as a navigation controller. 我有初始视图作为导航控制器。 This controller calls several views but the view that I'm having an issue with is a UICollectionViewController.
这个控制器调用了几个视图,但我遇到问题的视图是一个UICollectionViewController。 This is called from a button click on the first viewController connected to the navigationController.
这是通过单击连接到navigationController的第一个viewController上的按钮来调用的。 I then have a segue setup that goes from the UICollectionViewCell to a new ViewController.
然后我有一个从UICollectionViewCell到新的ViewController的segue设置。 So I'm assuming this is a second level NavigationController.
所以我假设这是一个二级NavigationController。
Secondly I have a Back Button Image that is displayed on my navigation instead of the text < Back. 其次我有一个后退按钮图像显示在我的导航而不是文本<返回。
The back button shows fine in the navigation controller on all the views except for the one called from the second level segue. 除了从第二级segue调用的视图外,后退按钮在所有视图的导航控制器中都显示正常。
As I understand from reading other posts on this topic it is because a new navigation Controller is created on the second level segue. 正如我从阅读有关该主题的其他帖子中所理解的那样,这是因为在第二级segue上创建了新的导航控制器。
I'm fine with that because everything works fine but I need the button to show on that second navcontroller. 我很好,因为一切正常但我需要按钮显示在第二个navcontroller上。
I was trying to programmatically add the button to the navigation controller associated with the current view. 我试图以编程方式将按钮添加到与当前视图关联的导航控制器。 I am using this code:
我正在使用此代码:
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)
}
This code does not seem to add the button to the current navigation controller. 此代码似乎没有将按钮添加到当前导航控制器。
This project is using Swift 3 and Xcode 8 这个项目使用的是Swift 3和Xcode 8
Use 采用
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: nil, action: nil)
instead of 代替
self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: nil, action: nil)
The reason for this is explained in this link . 在此链接中解释了其原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.