弹出回到视图控制器时使用ARC的EXC_BAD_ACCESS

时间:2014-01-13 18:56:57

标签: 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 开源控件。使用分离视图控制器我加载到下拉控制器。此代码位于名为 RootViewController 的视图控制器中,该控制器嵌入在 UINavigationController 中。然后有一种方法可以在导航控制器中推送另一个视图控制器。当我尝试使用该方法(在 AnotherViewController 中)popToRootViewController:时,我的应用程序崩溃并且EXC_BAD_ACCESS消息出现在控制台中。

修改

这是我在“AnotherViewController”中的代码

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

1 个答案:

答案 0 :(得分:1)

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

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