user2787386
user2787386

Reputation: 303

App delegate: unrecognised selector sent to instance

I've got some code in my app delegate method that is supposed to send an object to a view controller. For some reason though it is throwing an error : "unrecognized selector sent to instance"

in the view controller I have the following variable set:

@property (nonatomic, strong) Patient* patient;

the code in my app delegate looks like this:

    UISplitViewController* splitViewController = (UISplitViewController*)self.window.rootViewController;
UINavigationController* patientNavController = [splitViewController.viewControllers objectAtIndex:0];
PatientMasterTableViewController* patientMasterTableViewController = (PatientMasterTableViewController*)[patientNavController topViewController];
PatientDetailViewController* patientDetailViewController = [splitViewController.viewControllers objectAtIndex:0];

Patient* firstPatient = [[patientMasterTableViewController patientArray] objectAtIndex:0];
[patientDetailViewController setPatient:firstPatient];// this line throwing the error

I am new to iOS and I don't quite understand why it won't allow me to pass the patient object. Can anyone help?

Upvotes: 0

Views: 905

Answers (2)

Alexey Lobanov
Alexey Lobanov

Reputation: 61

You have bug in your code - look at this two lines:

UINavigationController* patientNavController = [splitViewController.viewControllers objectAtIndex:0];

PatientDetailViewController* patientDetailViewController = [splitViewController.viewControllers objectAtIndex:0];

array splitViewController.viewControllers contains only ONE ! element in portrait mode and TWO elements in landscape mode. DetailsViewController always is in this array but master does not: in portrait mode array contains only detail view ctrl and in landscape the array is @[master,details]

so if You want always to get DetailViewController use this code

[[splitViewController viewControllers] lastObject];

Upvotes: 0

Abhi Beckert
Abhi Beckert

Reputation: 33359

This line of code:

PatientDetailViewController* patientDetailViewController = [splitViewController.viewControllers objectAtIndex:0];

Is not guaranteed to return a PatientDetailViewController. It can return an object of any class, and you are not checking what class of object is returned.

Your app is crashing because it's returning a UINavgationController object, which does not have a setPatient method.

As for why it's returning an object of the wrong class, that will depend what view controllers you have created.

Upvotes: 1

Related Questions