Reputation: 1
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.
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
Reputation: 131481
If you look at the error you are getting, it says the IBAction it can't find is -[UIViewController ShowOrangeViewController:]:
.
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:
I'm guessing it says "UIViewController ShowOrangeViewController", which is wrong. If so:
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.
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