ios - 弹回 View Controller 时使用 ARC 的 EXC_BAD_ACCESS

标签 ios objective-c automatic-ref-counting exc-bad-access

首先,这是一些代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    FirstViewController *first = [[FirstViewController alloc] init];
    SecondViewController *second = [[SecondViewController alloc] init];
    MBPullDownController *pullDownController = [[MBPullDownController alloc] initWithFrontController:first backController:second];
    [self.navigationController addChildViewController:pullDownController];
}

- (void)pushAnotherViewController:(NSNotification *)notification
{
    AnotherViewController *another = [self.storyboard instantiateViewControllerWithIdentifier:@"anotherViewController"];
    [self pushScheduleViewController:another];
}

我使用MBPullDownController 开源控件。使用分离 View Controller 我加载到下拉 Controller 。此代码位于名为 RootViewController 的 View Controller 中,该 View Controller 嵌入到 UINavigationController 中。然后是在导航 Controller 中推送另一个 View Controller 的方法。当我尝试使用方法(在 AnotherViewController 中)popToRootViewController: 时,我的应用程序崩溃并且 EXC_BAD_ACCESS 消息出现在控制台中。

编辑

这是我在“AnotherViewController”中的代码

- (void)popBack
{
    RootScheduleViewController *root = [[RootScheduleViewController alloc] init];
    [self.navigationController popToViewController:root animated:YES];
}

最佳答案

当您调用 popBack 时遇到错误访问错误,因为您正在创建 View Controller 的新实例,然后尝试弹出到它。对于导航 Controller , View Controller 必须是导航堆栈的一部分才能弹出。因此,如果此 View Controller 的实例存在,请在导航堆栈中找到它并弹出它。

for(UIViewController * viewController in self.navigationController.viewControllers){
  if([viewController isKindOfClass:[RootScheduleViewController class]]){
    [self.navigationController popToViewController:viewController animated:NO];
    break;
  }
}

关于ios - 弹回 View Controller 时使用 ARC 的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21099131/

相关文章:

ios - 可重复使用的 MBProgressHUD

iphone - 将 NSString 拆分为核心数据

python - 如何处理返回 NSError 的 PyObjC 方法?

ios - 圆弧 : Setting compiler flags to -fno-objc-arc and build errors

ios - C 回调中的 ARC 弱引用

ios - 创建一个 block 并将其命名为输入 block ?

ios - [Swift]如何将字符串分配给 UITextField?

android - 将 Qt 网络功能与不同 OpenSSL 版本一起使用

iOS 数据保护不适用于已安装的应用程序

Objective-C 枚举、NS_ENUM 和 NS_OPTIONS