![](/img/trans.png)
[英]Black screen when dismissing a UIViewController with custom transition from within a UINavigationController
[英]Wrong target when calling method in custom UINavigationController from UIViewController
我創建了UINavigationController的子類,並將UIBarButtonItem放在導航欄中。 代碼如下:
SharedNavigationController.swift
func showNextButton(nc: UINavigationController) {
print(nc)
let nextButton = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Plain, target: nc, action: "next")
print(nextButton.target)
self.navigationBar.topItem?.rightBarButtonItem = nextButton
}
但是,點擊按鈕時目標位置錯誤,我遇到了問題。 目標不是導航控制器(在其中創建該類的類),而是導航控制器中的當前視圖控制器。 結果,出現以下錯誤:
***由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'-[[App.SignupBasicInfoForm next]:無法識別的選擇器發送到實例0x7af7d0c0'
我最初將目標設置為“ self”,但它是在導航控制器中引用視圖控制器,而不是導航控制器本身。 我也嘗試將目標設置為self.navigationController
,效果相同。 最后,我將SharedNavigationController
的實例顯式傳遞給showNextButton
方法,如上面的代碼所示。 打印語句給我這個:
print(nc): <App.SharedNavigationController: 0x7d41f800>
print(nextButton.target): Optional(<App.SharedNavigationController: 0x7d41f800>)
因此至少在SharedNavigationController
,目標是正確的。 但是,當從導航控制器中的當前視圖控制器中輕按按鈕時,似乎目標已更改。 我上面嘗試過的沒有用。 我如何為按鈕指定正確的目標,而不必定義方法或從視圖控制器而不是導航控制器設置條形按鈕項目?
您可能需要在子類中覆蓋此func以執行所需的操作
override func pushViewController(viewController: UIViewController, animated: Bool) {
viewController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Plain, target: nc, action: "next")
super.pushViewController(viewController, animated: animated)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.