gpt4 book ai didi

ios - 由于未捕获的异常 'NSInvalidArgumentException' 终止应用程序,原因 : '-[_. AppDelegate 添加:]:无法识别的选择器

转载 作者:可可西里 更新时间:2023-11-01 00:58:33 25 4
gpt4 key购买 nike

我创建了一个名为 Main.storyboard 的 Storyboard,并创建了一个 Storyboard ID 为“addBtnVC”的 AddBtnViewController。在 App Delegate 中,我以编程方式初始化了一个带有三个 View Controller 的选项卡栏。对于其中一个选项卡栏 View Controller ,我创建了一个添加按钮,该按钮将以编程方式转换为 AddBtnViewController。但是,我收到此错误:

uncaught exception 'NSInvalidArgumentException', reason: '-[_.AppDelegate add:]: unrecognized selector sent to instance 0x7f91cb422a30'

我的代码:

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)
}
}
}

如何修复此错误?

为了后代:

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)
}

}

最佳答案

您将按钮操作的目标 设置为代表AppDelegate 类的self

按钮操作的选择器 必须与目标属于同一类,但事实并非如此。这就是错误消息所说的。此外,选择器的签名是错误的,因为它只接受一个参数。

关于ios - 由于未捕获的异常 'NSInvalidArgumentException' 终止应用程序,原因 : '-[_. AppDelegate 添加:]:无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39196855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com