UIViewController throws unrecognized selector exception to prepareForSegue

I am trying to follow Stanford CS193p iOS programming lectures. One of the demos called Happiness creates two UIViewControllers, a PsychViewController and a HappinessViewController. It moves from the PsychViewController to the HappinessViewController using the target action method.

The following code continues to throw this exception: "[UIViewController setHappiness:]: unrecognized selector sent to the instance"

Here's the abusive code:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"ShowDiagnosis"]) { [segue.destinationViewController setHappiness:7]; } } 

I looked for this site and others, and usually, when this error occurs, this is due to the fact that the general UIViewController was not set correctly for the specific view controller object used in the program, in this case "HappinessViewController". But I set the generic UIViewController as a HappinessViewController using the identity inspector in IB, and I still get the exception. I am tearing my hair out if someone can help, it will be very appreciated.

+7
source share
8 answers

I understood the problem. Although I correctly pointed out that the corresponding UIViewController was a HappinessViewController, the linker, for some reason, did not reference the correct files. The fix was to double-click the .xcodeproj file inside Xcode, then go to the "Assembling phases" section and manually add the files in the "Compile Sources" section.

+7
source

Let's look at the exception:

 -[UIViewController setHappiness:]: unrecognized selector sent to instance 

This tells you two things about an unrecognized message. It tells you the selector, setHappiness: It also tells you the actual, executable class of the receiver, UIViewController .

Of course, the UIViewController does not have a method called setHappiness: This is why you got the exception.

You wrote a subclass of UIViewController called HappinessViewController , which has a setHappiness: method (or a read / write property called happiness , which the method generates). And you want the receiver (destination view controller) to be an instance of this subclass ( HappinessViewController ).

But the exception tells you that the destination view controller is just a UIViewController . Therefore, even if you think that this is so, you probably did not specify a custom view controller class in the storyboard. You may have configured your own class for some other view controller, but you have not defined the class for this destination.

You need to set a custom destination controller class in the Identity Inspector, for example:

set custom class in identity inspector

+19
source

For me, the class of the view controller in the help panel has changed. enter image description here

+4
source

Try it like this:

 HappinessViewController *controller = segue.destinationViewController; [controller setHappiness:7]; 
+1
source

I had the same problem today, and that was because my custom class was in the library. The library itself was connected at the assembly stages, but this was not enough to pull out the class. So finally, I solved it by adding the following line to my AppDelegate.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [ CustomClass class ]; } 

This causes the linker to be pulled into the class. Otherwise, simply linking in the library may not be enough to pull out the class if it is not mentioned somewhere in the application.

0
source

I tried adding Cast and it worked for me, I had the same problem:

  FirstViewController *detailViewController = (FirstViewController *)[segue destinationViewController]; 
0
source

Check which prepareForSegue command is running. My problem was that in the viewController session and in the incoming viewController 2 sessions started. The solution and always good practice is to verify the segues identifiers.

 - (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"segueIdentifier"]) { } } 
0
source

Transfer before appointment

 HappinessViewController *controller = (HappinessViewController *)segue.destinationViewController; [controller setHappiness:7]; 

Make sure your HappinessViewController is set to your VC class in your storyboard.

-one
source

All Articles