我已经将一个导航栏添加到视图控制器的顶部。 我试图控制一个按钮是否基于条件可见,但我无法添加按钮。 到目前为止,我有,
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.
更多推荐
发布评论