Ios 尝试返回到开始:无法识别的选择器已发送到实例

Ios 尝试返回到开始:无法识别的选择器已发送到实例,ios,swift,instance,selector,rootviewcontroller,Ios,Swift,Instance,Selector,Rootviewcontroller,在结果页面之后,我希望用户按“完成”并返回列表上的第一个问题。此时,应用程序崩溃,并将“无法识别的选择器”发送到实例 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] ***

在结果页面之后,我希望用户按“完成”并返回列表上的第一个问题。此时,应用程序崩溃,并将“无法识别的选择器”发送到实例

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
    函数中遗漏了发件人的参数
  • 使用
    #选择器
    ,它是类型安全的,并且会引发编译错误
  • Objective-C方法需要加前缀
    @objc
按钮: 功能:
将准确错误的副本粘贴到您的问题中,而不是试图描述它。其中通常有关于帮助人们提供答案的选择器和预期类别的详细信息。使用
#selector
语法:
#selector(完成)
并用
@objc
@vadian标记操作不确定如何实现?将
选择器((“完成”)
替换为
选择器(完成)
在这种情况下,参数是可选的。
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done",
                                                    style: .plain,
                                                    target: self,
                                                    action: #selector(done(sender:)))
@objc func done(sender: UIBarButtonItem) {

    navigationController?.popToRootViewController(animated: true)

}