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

标签 ios swift appdelegate nsexception

我创建了一个名为 Main.storyboard 的 Storyboard,并创建了一个 Storyboard ID 为“addBtnVC”的 AddBtnViewController。在应用程序委托(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 add:]: 无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39196855/

相关文章:

ios - AppDelegate Swift 中的选项卡式应用程序和核心数据功能

ios - 使用Facebook登录时,FBSDKLoginManagerLoginResult isCancelled为true

ios - NSJSONSerialization 不更新(可能被缓存)

ios - 符合 Objective-C 协议(protocol)的 Swift 类

objective-c - 错误前预期的说明符限定符列表

ios - Swift - 在 AppDelegate.swift 中设置依赖注入(inject)

ios - iOS 上 OpenGL ES 3.0 中 texture3D 的插值

swift - 使用平移手势缩放 UITextView

ios - EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000200 向图像添加过滤器时

ios - 键盘不会随着 resignFirstResponder 消失