简体   繁体   English

从UIViewController调用自定义UINavigationController中的方法时目标错误

[英]Wrong target when calling method in custom UINavigationController from UIViewController

I created a subclass of UINavigationController and put a UIBarButtonItem in the navigation bar. 我创建了UINavigationController的子类,并将UIBarButtonItem放在导航栏中。 The code is as follows: 代码如下:

SharedNavigationController.swift 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
}

However, I am having problems with the button having the wrong target when it is tapped. 但是,点击按钮时目标位置错误,我遇到了问题。 Instead of the target being the navigation controller (the class in which it is created), it is instead the current view controller in the navigation controller. 目标不是导航控制器(在其中创建该类的类),而是导航控制器中的当前视图控制器。 As a result, I get the following error: 结果,出现以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[App.SignupBasicInfoForm next]: unrecognized selector sent to instance 0x7af7d0c0' ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[[App.SignupBasicInfoForm next]:无法识别的选择器发送到实例0x7af7d0c0'

I initially set the target to "self" but it is referencing the view controller within the navigation controller, rather than the navigation controller itself. 我最初将目标设置为“ self”,但它是在导航控制器中引用视图控制器,而不是导航控制器本身。 I also tried setting the target as self.navigationController , with the same effect. 我也尝试将目标设置为self.navigationController ,效果相同。 Finally, I explicitly passed the instance of SharedNavigationController to the showNextButton method, as shown in the code above. 最后,我将SharedNavigationController的实例显式传递给showNextButton方法,如上面的代码所示。 The print statements give me this: 打印语句给我这个:

print(nc): <App.SharedNavigationController: 0x7d41f800>
print(nextButton.target): Optional(<App.SharedNavigationController: 0x7d41f800>)

So at least in SharedNavigationController , the target is correct. 因此至少在SharedNavigationController ,目标是正确的。 But when the button is tapped from the current view controller inside the navigation controller, it seems like the target has changed. 但是,当从导航控制器中的当前视图控制器中轻按按钮时,似乎目标已更改。 What I tried above did not work. 我上面尝试过的没有用。 How can I specify the correct target for the button, without having to define the method or set the bar button item from the view controller rather than the navigation controller? 我如何为按钮指定正确的目标,而不必定义方法或从视图控制器而不是导航控制器设置条形按钮项目?

you may need to override this func in your subclass to do what you want 您可能需要在子类中覆盖此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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM