A problem setting a value of a class that is being segued to in Objective-C

Hi,


I have 2 ViewControllers and a Segue between them that I trigger programmatically.


The first ViewController has some data stored in self.myCustomProperty that it needs to pass to the 2nd controller. The second ViewController with class name "MyTableViewController" which inherits from UITableViewController has a property customData which implies also the corresponding setter method "setCustomData:somedata".


The first ViewController performs the Segue on a certain event:


[self performSegueWithIdentifier:@"MyCustomSegue" sender:self];

which starts this:


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

 if( [segue.identifier isEqualToString:@"MyCustomSegue"] ){  

     MyTableViewController *destinationController = segue.destinationViewController;
  
     [destinationController setCustomData:self.myCustomProperty];  
 }  
} 

The problem is that [destinationController setCustomData:self.myCustomProperty] causes


'NSInvalidArgumentException', reason: '-[UITableViewController setCustomData:]: unrecognized selector sent to instance 0x...


After inspecting the problem I noticed that destinationController variable has the class "UIViewController" class instead of "MyTableViewController". I suppose, the exception is thrown, because UIViewController does not have the customData property.


I have searched for some time in Internet but neither did I find mentions of such problems nor did I find a way to turn UIViewController into MyTableViewController. The only method to do the latter that I have found is "object_setClass(id obj, Class cls)", but using it can cause serious problems.


How can I solve the issue?

>> The second ViewController with class name "MyTableViewController"


The way you get the segue destination to actually be of class MyTableViewController is to set its class name in IB. Select the destination controller there, choose the Identity inspector (the third icon in the inspector panel) and type in the class name in the top text field (or choose the name from the popup). When you hit enter, it should automatically check the "Inherit module from target" box.


If you've done this already, then there is something more subtle happening, but it's certainly the storyboard that's wrong.

Unfortunately, on my target view controller in the identity inspector I have the correct value for "Class" set and the "Inherit Module from Target" is also checked. Actually, when I change the custom class to something else and then type the correct one again and press enter, the "Inherit Module from Target" is not being checked automatically. I have to check it.


Additional info:

- the segue is presented modally

- the .m file has the correct target set

- I cannot set the target at .h file (I guess it's correct)

- when I start entering the target controller's class name in the identity inspector, it is correctly autocompleted.

That's unfortunate, because you have hard evidence that the object being created is of class UIViewController. The '-[UITableViewController setCustomData:]: unrecognized selector sent to instance 0x…' message always shows the actual class of the object.


The trick here is to discover why it's wrong.


Try unchecking "Inherit Module from Target". I forgot this was Obj-C, not Swift, and Obj-C class names are not namespaced by module like Swift's are. I just assumed Obj-C would do the right thing whether it was checked or unchecked, but maybe it matters.


Let's hope that's all it is. Otherwise, I'm out of ideas, because the rest of your setup looks correct.

Hi rreimche, maybe it isn't true but just try again to search in whole project this string "MyCustomSegue" and see if is there any other controller is set this Segue indentifier in interface builder. I've faced to something similar to this bug because I set the same segue identifier for two view controllers.

A problem setting a value of a class that is being segued to in Objective-C
 
 
Q