导航控制器看不到右栏按钮项



我试图通过允许用户添加算命应用程序的新结果来理解演示视图控制器。当用户点击导航栏右上角的"添加新结果"时,会弹出一个新的视图控制器,用户可以在此处输入新结果。我正在尝试在演示控制器的右上角添加一个完成按钮,但收到一条错误消息,说Value of type 'UINavigationController?' has no member 'rightBarButtonItem'.

我以为这是因为引用topViewController是问题所在,但我正在1个ViewController中完成所有这些工作。

@IBAction func actionButtonTapped(_ sender: UIBarButtonItem) {
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "AddNewOptionController")
viewController.modalPresentationStyle = .popover
let popover : UIPopoverPresentationController = viewController.popoverPresentationController!
popover.barButtonItem = sender
popover.delegate = self
present(viewController, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .fullScreen
}
func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
let navigationController = UINavigationController(rootViewController: controller.presentedViewController)
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(ViewController.dismissViewController))
//error below
navigationController.topViewController?.navigationController.rightBarButtonItem = doneButton
return navigationController
}
func dismissViewController() {
self.dismiss(animated: true, completion: nil)
}

而不是:

navigationController.topViewController?.navigationController.rightBarButtonItem = doneButton

尝试:

navigationController.topViewController?.navigationItem.rightBarButtonItem = doneButton

您可以使用以下代码:

self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named:"YOUR_IMAGE_NAME"), style: .plain, target: self, action: #selector(YOUR_FUNCTION))

希望这对你有用

最新更新