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

标签 ios objective-c uinavigationcontroller uistoryboard

我有一个在 View 之间使用 UINavegationController 的项目:

enter image description here

我正在使用导航 Controller ,因为我正在使用模态呈现(翻转)

但我的问题是我试图将 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!";
    }
}

但是我收到了这个错误:

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

你们中有人知道这个错误的原因或解决方法吗?

非常感谢你的帮助

最佳答案

它崩溃了,因为segue上的destinationViewController实际上是一个UINavigationController,而不是BViewControllerUINavigationControllers不知道属性 comingFromA。您可以像这样替换 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!";
    }
}

关于iOS:将对象传递给 viewController 错误:无法识别的选择器已发送至实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40619261/

相关文章:

ios - SpriteKit 接头

ios - 来自 View Controller 的带有提示的 UINavigationController 动画

ios - 在导航到另一个 View Controller 之前切换标签栏

ios - UIView 重写 func TouchBegan 默认 stub 抛出错误

ios - 如何更改 TabBarItem 的背景颜色

iphone - UIImageView 转换缩放

ios - 如何正确推送 JSQMessagesViewController

objective-c - FBSDKLoginManager错误代码308并且单例无法解决它

ios - UINavigationController 实例化后 UISearchbar 不可见

当登录页面存在时,iOS 快速可达性应该失败