简体   繁体   English

iOS:将对象传递给viewController错误:无法识别的选择器已发送到实例

[英]iOS: passing object to viewController error: unrecognized selector sent to instance

I have project where I'm using UINavegationController between to views: 我有我在视图之间使用UINavegationController的项目:

在此处输入图片说明

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: 但是我的问题是我试图将NSString对象传递给ViewControllerB:代码:

- (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 非常感谢您的帮助

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 . 之所以崩溃,是因为segue上的destinationViewController实际上是一个UINavigationController而不是BViewController ,并且UINavigationControllers不知道属性comingFromA You can test this theory by replacing prepareWithSegue like this: 您可以通过如下方式替换prepareWithSegue来测试该理论:

- (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!";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM