【发布时间】:2020-10-06 02:20:27
【问题描述】:
我有一个现有的导航控制器委托,它在应用程序的每个视图控制器上放置一个菜单按钮。
class MyNavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let navItem = viewController.navigationItem
let menuBtn = MyCustomMenuButton()
...
navItem.setRightBarButton(menuBtn, animated: false)
}
这很好用...我在每个视图的导航栏中都有一个菜单按钮。但是对于某些视图,我想在菜单按钮旁边的右侧添加另一个按钮,所以我添加了这个:
class CustomViewController: UIViewController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let newButton = UIBarButtonItem(title: "(+)", style: .plain, target: nil, action: nil)
self.navigationItem.rightBarButtonItem = newButton
}
}
但这没有任何效果。菜单按钮仍然存在,但未添加新按钮。那应该怎么做呢?
【问题讨论】:
-
我可以覆盖
CustomViewController中的UINavigationControllerDelegate,但这是最好/唯一的方法吗?