无法识别的选择器发送到实例 Swift 2.1

Posted

技术标签:

【中文标题】无法识别的选择器发送到实例 Swift 2.1【英文标题】:Unrecognized selector sent to instance Swift 2.1 【发布时间】:2016-05-13 18:47:54 【问题描述】:

每次保存任务时,以下代码都会导致崩溃。新数据需要输入到Detail View控制器并保存。这有效,但应用程序每次都会终止。请告诉我为什么这不起作用:

代码:

var detailViewController: DetailViewController? = nil
var managedObjectContext: NSManagedObjectContext? = nil


override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem()

    let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addNewCourseWork")
    self.navigationItem.rightBarButtonItem = addButton
    if let split = self.splitViewController 
        let controllers = split.viewControllers
        self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
    

    //hide blank cells
    let backgroundView = UIView(frame: CGRectZero)
    tableView.tableFooterView = backgroundView

    // register  for receiving notification, I need to update detail view from this exact same class, not just any MasterView class instance...
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"MasterViewController.pushNewValuesToDetail", name: "updateDetailTableValues", object: nil)


func pushNewValuesToDetail() 
    self.performSegueWithIdentifier("showDetail", sender: nil)

错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- MasterViewController.pushNewValuesToDetail]: unrecognized selector sent to instance 0x7fc622d1b820

【问题讨论】:

unrecognized selector sent to instance的可能重复 如果问题已解决,请随时将问题标记为已解决。 【参考方案1】:

试试

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(pushNewValuesToDetail), name: "updateDetailTableValues", object: nil)

不需要注意基类,新的创建选择器的方法(在Xcode最新版本中)是#selector

【讨论】:

以上是关于无法识别的选择器发送到实例 Swift 2.1的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3 NSCoder 无法识别的选择器发送到实例

Swift:手势识别器无法识别的选择器发送到实例

Swift无法识别的选择器发送到实例

无法识别的选择器发送到实例 - Swift

Swift - 无法识别的选择器发送到 SDWebImage 方法上的实例

NSNotification 无法识别的选择器发送到 Swift 中的实例