user2924482
user2924482

Reputation: 9140

iOS: passing object to viewController error: unrecognized selector sent to instance

I have project where I'm using UINavegationController between to views:

enter image description here

I'm using the navigation controller because I'm using modal presentation (Flip)

But my problem is I'm trying to pass NSString Object to ViewControllerB: Code:

- (IBAction)goToB:(id)sender {

    [self performSegueWithIdentifier:@"goToB" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"goToB"]) {

        BViewController *vc = (BViewController*)[segue destinationViewController];
        vc.commigFromA = @"I'm going to B!";
    }
}

But I'm getting this error:

-[UINavigationController setCommigFromA:]: unrecognized selector sent to instance 0x7ff1c4830c00

Any of you knows why or a way around this error?

I'll really appreciate your help

Upvotes: 0

Views: 90

Answers (1)

Andy Obusek
Andy Obusek

Reputation: 12842

It's crashing because the destinationViewController on the segue is actually a UINavigationController, not a BViewController, and UINavigationControllers don't know about a property comingFromA. You can test this theory by replacing prepareWithSegue like this:

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

        UINavigationController *nvc = (UINavigationController*)[segue destinationViewController];
        BViewController *vc = (BViewController*)nvc.viewControllers[0];
        vc.commigFromA = @"I'm going to B!";
    }
}

Upvotes: 1

Related Questions