Swift 3.0向导航栏添加一个正确的按钮(Swift 3.0 Adding a Right Button to Navigation Bar)

编程入门 行业动态 更新时间:2024-10-11 07:34:03
Swift 3.0向导航栏添加一个正确的按钮(Swift 3.0 Adding a Right Button to Navigation Bar)

我已经将一个导航栏添加到视图控制器的顶部。 我试图控制一个按钮是否基于条件可见,但我无法添加按钮。 到目前为止,我有,

var addButton: UIBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped)) override func viewDidLoad() { super.viewDidLoad() let boool = true if boool { self.navigationItem.rightBarButtonItem = self.addButton } else { self.navigationItem.rightBarButtonItem = nil } } func addTapped(sender: AnyObject) { print("hjxdbsdhjbv") }

我相信它不能正常工作,因为我在VC中添加了一个导航栏,而不是使用导航控制器并在那里使用栏。 我想知道是否有一种方法来处理这个导航栏。

I have added a navigation bar to the top of a view controller. I am trying to control whether a button is visible based a condition, but I am having trouble adding the button. So far I have,

var addButton: UIBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped)) override func viewDidLoad() { super.viewDidLoad() let boool = true if boool { self.navigationItem.rightBarButtonItem = self.addButton } else { self.navigationItem.rightBarButtonItem = nil } } func addTapped(sender: AnyObject) { print("hjxdbsdhjbv") }

I believe it is not working properly because I have added a navigation bar into the VC, instead of using a navigation controller and working with the bar there. I was wondering if there was a way to work with this navigation bar.

最满意答案

你说你通过storyboard添加了一个UINavigationBar到你的视图控制器,但是看看你提供的代码在你的IB导航栏中没有出口连接。

为了访问self.navigationItem您的视图控制器必须嵌入到UINavigationController或者成为层次结构的一部分。 除非您需要在单个视图控制器上使用自定义导航栏,否则我建议将其从Interface Builder中移除,然后确保所讨论的视图控制器嵌入在UINavigationController或者将其从另一个控制器中推入导航堆栈中它嵌入在导航控制器中,然后你应该看到你的UIBarButtonItem 。

You say you added a UINavigationBar to your view controller via storyboard, but looking at the code you provided there is no outlet connection to your navigation bar in IB.

In order to access self.navigationItem your view controller must be embedded in a UINavigationController or be part of a hierarchy which is. Unless you have a need for a custom navigation bar on an individual view controller, I suggest removing that from Interface Builder, then making sure either the view controller in question is embedded in a UINavigationController or it is being pushed onto the navigation stack from another controller which is embedded in a navigation controller and then you should see your UIBarButtonItem.

更多推荐

本文发布于:2023-07-19 04:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1173706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   正确   Swift   Bar   Navigation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!