So, I was reading this related SO, as it ended up being something I wanted to do as well.
I added a property to my destinationViewController’s header file:
@property (nonatomic, strong) NSString *incomingSegue;
And I’ve got it synthesized in the destinationViewController’s implementation file:
@synthesize incomingSegue = _incomingSegue;
I’ve added the following lines to my prepareForSegue method, for the sourceViewController (depending on which is triggering the segue):
[segue.destinationViewController setIncomingSegue:@"edit"];
[segue.destinationViewController setIncomingSegue:@"add"];
And, finally, I’ve got a process to check for which value is set in my destinationViewController’s implementation file:
if (_incomingSegue == @"add")
{
//snipped code here
}
else if (_incomingSegue == @"edit")
{
//snipped code here
}
So, apparently I am missing something. When I try to perform the segue I get an error that shows up about 1000 times in SO, which makes it rather difficult to figure out which detail I’ve overlooked. This thing triggers (according to breakpoints) in my prepareForSegue method on my sourceViewController:
unrecognized selector sent to instance
Can I not use a literal string (@”string”) in place of a (NSString *), or is it something else throwing the error?
Update (solved):
More detailed description of my prepareForSegue method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"editSegue"])
{
//snipped
[segue.destinationViewController setIncomingSegue:@"edit"];
}
else if ([[segue identifier] isEqualToString:@"addSegue"])
{
//snipped
DestinationViewController *dtv = (DestinationViewController *)[[segue destinationViewController]topViewController];
[dvc setIncomingSegue:@"add"]
}
}
Turns out, I had to use my declared DestinationViewController class object to set the value. Instead of just referencing the segue.destinationViewController as I did for the editSegue. I do not have a DestinationViewController class object declared for editSegue, so that one was/is working as intended.
You should add a condition around the line that sets the incoming segue:
The idea is to call
setIncomingSegue:only on the destination view controller that supports your added method.You should also change the code in the destination view controller to check string equality with
isEqualToString: