试图 return 开始:无法识别的选择器发送到实例

trying to return to start: unrecognized selector sent to instance

在结果页面之后,我希望用户按 "done" 和 return 到列表中的第一个问题。目前,应用程序崩溃并显示“无法识别的选择器发送到实例。

2018-04-19 11:56:49.072392-0400 test[62142:269319] -[test.ResultsController done]: unrecognized selector sent to instance 0x7f8c62f43b10
2018-04-19 11:56:49.078012-0400 test[62142:269319] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[test.ResultsController done]: unrecognized selector sent to instance 0x7f8c62f43b10'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000114a8b1e6 __exceptionPreprocess + 294
1   libobjc.A.dylib                     0x000000011074d031 objc_exception_throw + 48
2   CoreFoundation                      0x0000000114b0c784 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   UIKit                               0x000000011192c9eb -[UIResponder doesNotRecognizeSelector:] + 295
4   CoreFoundation                      0x0000000114a0d898 ___forwarding___ + 1432
5   CoreFoundation                      0x0000000114a0d278 _CF_forwarding_prep_0 + 120
6   UIKit                               0x00000001116ff6f8 -[UIApplication sendAction:to:from:forEvent:] + 83
7   UIKit                               0x0000000112104271 __45-[_UIButtonBarTargetAction _invoke:forEvent:]_block_invoke + 154
8   UIKit                               0x00000001121041aa -[_UIButtonBarTargetAction _invoke:forEvent:] + 154
9   UIKit                               0x00000001116ff6f8 -[UIApplication sendAction:to:from:forEvent:] + 83
10  UIKit                               0x000000011187aab4 -[UIControl sendAction:to:forEvent:] + 67
11  UIKit                               0x000000011187add1 -[UIControl _sendActionsForEvents:withEvent:] + 450
12  UIKit                               0x0000000111879d19 -[UIControl touchesEnded:withEvent:] + 580
13  UIKit                               0x00000001117743cf -[UIWindow _sendTouchesForEvent:] + 2729
14  UIKit                               0x0000000111775ad1 -[UIWindow sendEvent:] + 4086
15  UIKit                               0x0000000111719620 -[UIApplication sendEvent:] + 352
16  UIKit                               0x000000012ed78e6b -[UIApplicationAccessibility sendEvent:] + 85
17  UIKit                               0x000000011205a717 __dispatchPreprocessedEventFromEventQueue + 2796
18  UIKit                               0x000000011205d32c __handleEventQueueInternal + 5949
19  CoreFoundation                      0x0000000114a2dbb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
20  CoreFoundation                      0x0000000114a124af __CFRunLoopDoSources0 + 271
21  CoreFoundation                      0x0000000114a11a6f __CFRunLoopRun + 1263
22  CoreFoundation                      0x0000000114a1130b CFRunLoopRunSpecific + 635
23  GraphicsServices                    0x00000001184aea73 GSEventRunModal + 62
24  UIKit                               0x00000001116fe367 UIApplicationMain + 159
25  test                                0x000000010fe329e0 main + 48
26  libdyld.dylib                       0x0000000115c6c955 start + 1
27  ???                                 0x0000000000000001 0x0 + 1

) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)

我读到问题可能是它在代码中的位置,但它似乎没问题?

按钮的创建如下:

        override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: Selector(("done")))

        navigationItem.title = "Results"

        view.backgroundColor = UIColor.white

        view.addSubview(resultsLabel)
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": resultsLabel]))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": resultsLabel]))

        let names = ["Ross", "Joey", "Chandler", "Monica", "Rachel", "Phoebe"]

        var score = 0
        for question in QuestionController.questionsList {
            score += question.selectedAnswerIndex!
        }

        let result = names[score % names.count]
        resultsLabel.text = "Congratulations! \(result)."
    }

    func done() {
        navigationController?.popToRootViewController(animated: true)

两点注意事项:

  • 您在 done 函数中错过了发件人的参数
  • 使用#selector,类型安全但会抛出编译错误
  • Objective-C 方法需要加上 @objc
  • 前缀

按钮:

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done",
                                                    style: .plain,
                                                    target: self,
                                                    action: #selector(done(sender:)))

函数:

@objc func done(sender: UIBarButtonItem) {

    navigationController?.popToRootViewController(animated: true)

}