2017-08-25 76 views
0

我試圖通過允許用戶添加算命應用程序的新結果來了解演示視圖控制器。當用戶點擊導航欄右上角的「添加新結果」時,會彈出一個新的視圖控制器,這是用戶可以輸入新結果的位置。我正在嘗試在演示文稿控制器的右上方添加完成按鈕,但我收到一條錯誤消息,內容爲Value of type 'UINavigationController?' has no member 'rightBarButtonItem'NavigationController沒有看到正確的欄按鈕項目

我認爲這是因爲引用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) 
} 

回答

2

相反的:

navigationController.topViewController?.navigationController.rightBarButtonItem = doneButton 

嘗試:

navigationController.topViewController?.navigationItem.rightBarButtonItem = doneButton 
+0

哇,工作。你能解釋爲什麼這有效嗎? – LampPost

+0

像導航控制器堆棧中的每個視圖控制器都是特定於右欄按鈕項的導航項目。所以你不能在導航控制器本身中設置它們,這就是你的代碼試圖做的事情。 – bjd23

0

您可以使用此代碼:

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

跳e這項工作爲你