Diana Dasha
Diana Dasha

Reputation: 1

I can't understand what the problem with the Navigator Controller

I have just started working with Xcode(I use Xcode 15.0.1) and wanted to create a small app to understand transitions between ViewControllers. Following the code from an online video, I encountered an error. I tried to solve the issue myself and used different ways to write the same action. However, I kept getting the same error every time. I wanted to make it so that from the BlueViewController, when I press the 'Orange' button, there would be a transition to the OrangeViewController using a Navigation Controller. This is the code in BlueViewController.

@IBAction func ShowOrangeViewController(_ sender: Any) {
        let orangeVC = self.storyboard?.instantiateViewController(withIdentifier: "OrangeViewController") as! OrangeViewController
        self.navigationController?.pushViewController(orangeVC, animated: true);
    }
    
} 

This is what Main looks like.

Screens

When I run the program and press the Orange button, it triggers an error.

Thread 1: "-[UIViewController ShowOrangeViewController:]: unrecognized selector sent to instance 0x103b06a2

Upvotes: 0

Views: 103

Answers (1)

Duncan C
Duncan C

Reputation: 131481

If you look at the error you are getting, it says the IBAction it can't find is -[UIViewController ShowOrangeViewController:]:.


Edit:

I created a project with your setup, got it working, and then commented out my IBAction in my BlueViewController. The error I got was "*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PushFromButton.BlueViewController handleOrangeButton:]: unrecognized selector sent to instance 0x102907fb0'"

Note that it lists the target object as BlueViewController, not UIViewController. (That is the class name for my Blue view controller. PushFromButton is the name of the app.) Your error says the target is UIViewController, a generic view controller.


It's trying to find your ShowOrangeViewController(_:) method in the base UIViewController class. Your "Blue View Controller" is probably of a custom class "BlueViewController", but the IBAction is trying to invoke the method in the wrong class.

Do these things:

  • Open your storyboard and select the "View Controller Scene" for the blue view controller. Then select the "identity inspector" and make sure the class of that view controller shows as "BlueViewController" (or whatever class name you gave your blue view controller.)
  • Switch to the Connections Inspector, select the "Orange" button in your Blue View controller, and check the IBAction for the button.

I'm guessing it says "UIViewController ShowOrangeViewController", which is wrong. If so:

  • Click the x in the connections inspector under the events for the orange button to remove the bad IBAction link.
  • Option-click on your "OrangeViewController.swift" source file to open it in the auxiliary editor.
  • Control-drag from the orange button in the Interface builder onto your ShowOrangeViewController action and make sure it creates a link and now lists that IBAction correctly.

Re-run your program and try the orange button again.

Edit:

By the way, there are some strong naming conventions in Swift that you should learn to follow. One of those conventions is that function names and variable names should start with a lower-case letter. Only type names should start with a lower-case letter. Thus, your IBAction should be named showOrangeViewController(_:), not ShowOrangeViewController.

If you select the name "ShowOrangeViewController" in the BlueViewController's source file, then select "refactor>rename" from the editor menu, Xcode will do a smart rename of your IBAction, including any code that calls that method and fixing the link in your storyboard to use the new name.

Upvotes: -1

Related Questions