“UINavigationController set unrecognized selector sent to instance” when trying to set NSString in modal view – Xcode 6

I created a new view controller embedded in a UINavigationController in my storyboard and linked it with the class. I created a action segue to a button so that when it was clicked it should present the view controller modally. When I tried to setup the segue for it and update an NSString declared in the header file of that view controller I would get an error “-[UINavigationController setCurrentSpeaker:]: unrecognized selector sent to instance 0x14c634a70

This is the code I was using to update the currentSpeaker NSString:

if ([[segue identifier] isEqualToString:@"speakerSegue"]){
        SpeakerViewController *svc = [segue destinationViewController];
        svc.currentSpeaker = thisSpeaker;
 
    }

The problem with this code is that the segue’s destinationViewController is actually the navigation controller and not the SpeakerViewController I thought it was.
I updated it to this which worked perfectly:

if ([[segue identifier] isEqualToString:@"speakerSegue"]){
        UINavigationController *navController = (UINavigationController*)[segue destinationViewController];
        SpeakerViewController *svc = (SpeakerViewController *)[navController topViewController];
        svc.currentSpeaker = thisSpeaker;
 
    }

Leave a Comment