我想在viewWillAppear中更改左侧导航栏按钮项(需要反复更改该项,因此viewDidLoad不起作用)。 我在viewWillAppear中使用以下代码:
// There is a diff 'left bar button item' defined in storyboard. I'm trying to replace it with this new one
var refreshButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: {})
self.navigationController.navigationItem.leftBarButtonItem = refreshButton
// title and color of nav bar can be successfully changed
self.navigationController.navigationBar.barTintColor = UIColor.greenColor()
self.title = "Search result"
我使用调试器确保每一行代码都被执行。但是'leftBarButtonItem'没有更新,导航栏的内容却成功更新了。现在我已经束手无策了,有什么想法吗?谢谢!
viewWillAppear
中执行此操作,因为每次 VC 被呈现时都会调用它 - 使用类似viewDidLoad
的方法会更好。 - ZorayrviewWillAppear
,但是viewDidLoad
绝对是更好的位置放置这段代码。代码已更新。 - Imanou Petit