[英]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.