iOS 应用程序在最后一行 prepareForSegue 代码处终止。不能踏进去

标签 ios objective-c uinavigationcontroller uistoryboardsegue

我的应用程序有两个 ViewController,HomeViewController 和 AddActivityViewController,通过 HomeViewController 导航栏中的“+”按钮从后者切换到前者。

应用程序在点击 prepareForSegue 方法的最后一行时退出:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
        NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    if ([[segue identifier] isEqualToString:@"addActivity"])
    {
        AddActivityViewController *aavc = (AddActivityViewController *) [segue destinationViewController];
        aavc.delegate = self;
        ListActivity *addedActivity = (ListActivity *)[ListActivity MR_createInContext:localContext];
        aavc.thisActivity = addedActivity;
    }

}

我在这一行设置了一个断点:

aavc.thisActivity = addedActivity;

但是,一旦我尝试进入代码,应用程序就会退出,除了这个异常(exception):

2014-01-30 13:02:53.011 MRExample[31029:a0b] 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“- [UINavigationController setThisActivity:]: 无法识别的选择器发送到实例 0x8d93290'*

如果我注释掉违规行,segue 将执行。

我可以提供任何有助于澄清的代码。非常感谢所有帮助或见解!

最佳答案

从异常看来,aavc 嵌入在 UINavigationController 中。首先通过它获取对 aavc 的引用:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    if ([[segue identifier] isEqualToString:@"addActivity"])
    {
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        AddActivityViewController *aavc = (AddActivityViewController *)navController.topViewController;
        aavc.delegate = self;
        ListActivity *addedActivity = (ListActivity *)[ListActivity MR_createInContext:localContext];
        aavc.thisActivity = addedActivity;
    }
}

关于iOS 应用程序在最后一行 prepareForSegue 代码处终止。不能踏进去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21467109/

相关文章:

ios - 如何禁用 webView 中的弹出窗口?

iphone - mapkit ,用注释索引标记公开按钮

objective-c - 正确地将我的按钮链接到我的 View (在 UINavigationController 中)?

iphone - 为什么我用于 nav_bar 按钮的图像有白边?

swift - 呈现 activityViewController 时 UINavigationBar 隐藏

Xcode 9.0 中的 iOS 11.1 功能

c++ - 使用 env_time.cc :19:10: fatal error: 'windows.h' file not found 构建 Tensorflow iOS

ios - UITableViewCell 和 UILabel

ios - UITextView contentInset 不工作

ios - 域=NSPOSIXError域代码=61