简体   繁体   English

如何找到属于当前viewController的导航控制器?

[英]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.

相关问题 展开到不在当前导航视图控制器链中的viewController - Unwind to a viewController that is not in the current navigation view controller chain 如何为通过导航控制器连接的视图添加代码到viewcontroller.swift? - How do I add code to my viewcontroller.swift for views connected via a navigation controller? 目标 C:如何从当前 ViewController 后面的 ViewController 获取导航控制器 - Objective C: How to get navigation controller from ViewController behind my current ViewController 我如何选择另一个与当前根控制器共享相同根控制器的viewController? - How do I segue to another viewController that shares the same root controller as the current one? 如何找到iOS8上使用的当前键盘? - How do I find out the current keyboard used on iOS8? Swift &amp; Navigation:如何弹出直到某个 ViewController? - Swift & Navigation : How do I pop until a certain ViewController? 如何知道导航控制器上的哪个viewController? - How to know which viewController is on navigation controller? 如何使用Navigation Controller和ViewController处理多点触摸 - How to handle multiple touch with Navigation Controller and ViewController 如何从单独的视图控制器呈现导航控制器 - How to present navigation controller from a seperate viewcontroller 如何在导航控制器之前取消对ViewController的选择 - how to make a unwindsegue to viewcontroller before a navigation controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM