ios 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[_.AppDelegate add:]: 无法识别的选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39196855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_.AppDelegate add:]: unrecognized selector
提问by Amanda
I created a storyboard called Main.storyboard and created a AddBtnViewController with storyboard ID "addBtnVC". In the App Delegate, I initialized a tab bar with three view controllers programmatically. For one of tab bar view controllers, I created an add button that would transition to the AddBtnViewController programmatically. However, I received this error:
我创建了一个名为 Main.storyboard 的故事板,并创建了一个 AddBtnViewController,其故事板 ID 为“addBtnVC”。在 App Delegate 中,我以编程方式初始化了一个带有三个视图控制器的标签栏。对于选项卡栏视图控制器之一,我创建了一个添加按钮,该按钮将以编程方式转换到 AddBtnViewController。但是,我收到了这个错误:
uncaught exception 'NSInvalidArgumentException', reason: '-[_.AppDelegate add:]: unrecognized selector sent to instance 0x7f91cb422a30'
My code:
我的代码:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Make add btn for View Controller 1 of Navigation Bar
let addBtn = UIButton(frame: CGRectMake(0, 0, 30, 30))
addBtn.addTarget(self, action: #selector(ViewController1.add), forControlEvents: .TouchUpInside)
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = addBtn
NavController.navigationBar.topItem?.rightBarButtonItem = rightBarButton
return true
}
}
class ViewController1: UIViewController {
let addBtnVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("addBtnVC")
func add(sender: UIButton!) {
if let addBtnVC = storyboard!.instantiateViewControllerWithIdentifier("addBtnVC") as? AddBtnViewController {
presentViewController(addBtnVC, animated: true, completion: nil)
}
}
}
How do I fix this error?
我该如何解决这个错误?
For posterity:
为后人:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let VC1 = UIViewController()
//Make add btn for View Controller 1 of Navigation Bar
let addBtn = UIButton(frame: CGRectMake(0, 0, 30, 30))
addBtn.addTarget(VC1, action: #selector(VC1.add(_:)), forControlEvents: .TouchUpInside)
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = addBtn
NavController.navigationBar.topItem?.rightBarButtonItem = rightBarButton
return true
}
}
class ViewController1: UIViewController {
let addBtnVC:AddButtonViewController = AddButtonViewController()
func add(sender: UIButton!) {
let navController = UINavigationController(rootViewController: addBtnVC)
navController.navigationBar.tintColor = colorPalette.red
self.presentViewController(navController, animated: true, completion: nil)
}
}
采纳答案by vadian
You set the targetof the button action to self
which represents the AppDelegate class.
您设置表示 AppDelegate 类的按钮操作的目标self
。
The selectorof the button action must be in the same class as the target but it isn't. That's what the error message says. Further the signature of the selector is wrong because it takes one parameter.
按钮操作的选择器必须与目标在同一类中,但事实并非如此。这就是错误消息所说的。此外,选择器的签名是错误的,因为它需要一个参数。