iosswiftinstanceselectorrootviewcontroller

trying to return to start: unrecognized selector sent to instance


After the results page, I want the user to press "done" and return to the first question on the list. At the moment, the app crashes with "unrecognized selector sent to instance.

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: terminating with uncaught exception of type NSException (lldb)

I've read the issue might be its placement in the code, but it seems to be fine?

Here's the creation of the button:

        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)

And


Solution

  • Couple of points to note:

    Button:

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

    Function:

    @objc func done(sender: UIBarButtonItem) {
    
        navigationController?.popToRootViewController(animated: true)
    
    }