【问题标题】:getting access EXC_BAD_ACCESS when doing animation在做动画时获得 EXC_BAD_ACCESS
【发布时间】:2012-06-01 12:55:18
【问题描述】:

我正在制作动画,所以当您从第一个屏幕单击 MyAccount 按钮时,它会将您导航到帐户视图

- (void)pushToAccount
{
    AccountViewController *controller = [[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil];
    //[self.navigationController pushViewController:controller animated:NO];

    [UIView  beginAnimations: @"Showinfo"context: nil];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [self.view addSubview:controller.view];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
    [UIView commitAnimations];

}

但是,每当我点击 Account View Controller(其中包含一些按钮和图像视图)时,我的程序就会崩溃,在主类中显示 EXC_BAD_ACCESS

请就这个问题给我建议...

【问题讨论】:

  • 检查你的控制器的 viewDidLoad 是否被调用?
  • 是的,只要放一个断点就可以了
  • 从那里开始调试,向所有方法添加断点,并找出哪里出错了。

标签: ios exc-bad-access uiviewanimation


【解决方案1】:

您的视图控制器正在尝试通过添加另一个视图控制器的视图作为当前视图的子视图来为过渡设置动画。这在很多方面都是有问题的。值得注意的是,您并没有对新的视图控制器本身做任何事情......如果这是一个 ARC 项目,它将在您身上发布。

如果您只想从一个视图控制器转换到另一个视图控制器,您通常应该执行标准的pushViewController:animated:presentModalViewController:animated:。看起来你曾经做过前者。为什么要换成现在的代码?

如果您能告诉我们您想要做什么,为什么不使用标准转换,也许我们可以进一步帮助您。

更新:

如果您不喜欢默认动画,而是希望为您的过渡添加一些自定义动画,您可以执行以下操作:

CustomAnimationViewController *yourNewViewController = [[CustomAnimationViewController alloc] initWithNibName:@"CustomAnimationView" bundle:nil];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController:yourNewViewController animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
[UIView commitAnimations];

// if you're using ARC, you don't need this following release, but if you're not using ARC, remember to release it

// [yourNewViewController release];

更新 2:

并且,预计后续的逻辑问题,您如何为新视图控制器的解除设置动画,例如,您可能有一个“完成”按钮,该按钮调用如下方法:

- (IBAction)doneButton:(id)sender 
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelay:0.375];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView commitAnimations];
}

【讨论】:

  • 我想要做的是动画过渡。首先,我按照here 的教程进行操作,他们使用addSubview。但是。后来,我还在here 的stackOverFlow 中找到了另一个解决方案,他们都将动画转换。因此,当我这样做时,我在途中遇到了崩溃,但我根本没有解决该错误的方法
  • @ttran 我认为第一个解决方案非常糟糕(不是 ARC 友好,不保留 navigationController 堆栈等),但第二个解决方案是可靠的。我已经使用自定义动画相应地更新了我的答案,保留了 pushViewController 调用。我刚刚测试了它,它工作正常。
  • @ttran 预计您接下来会问如何为新视图控制器的解除设置动画,我已经相应地更新了我的答案。
【解决方案2】:

我的猜测是你有一个UIButton,它会在按下时调用你的pushToAccount 方法,而当按下它时它找不到该方法。这可能是因为方法签名不包含对象。因此,如果您将方法从

- (void)pushToAccount

- (void)pushToAccount:(id)sender

它可能会解决您的问题。确保包含pushToAccount 方法的对象在调用该方法之前没有被释放,这一点也很重要。或许在dealloc 中放置NSLog 消息或断点以确保它不会被调用。

【讨论】:

  • 我不这么认为,因为我毕竟可以导航到帐户视图控制器但在这里崩溃。
【解决方案3】:

我认为使用波纹管

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];

插入你的波纹管..

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];

希望对您有所帮助... :) 如果需要,动画也可以使用下面的代码......

CATransition *animation = [CATransition animation];
    [animation setDelegate:self];   
    [animation setType:kCATransitionFromBottom];
    [animation setDuration:1.0];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                                  kCAMediaTimingFunctionEaseInEaseOut]];
    [[self.window layer] addAnimation:animation forKey:kAnimationKey];

在这里使用您的窗口视图,否则窗口工作正常.. :)

【讨论】:

  • 我的帐户视图没有窗口属性。它是唯一的视图和一些控制器..
  • 是的。我没有做[self.view addSubview:controller.view];,而是使用[self.navigationController pushViewController: controller animated:NO];,它可以工作,但我根本不知道为什么
  • 对于动画,如果您在 delgate 方法中定义代码然后使用它,那么它的工作正常,否则使用 AppDelegate 对象,然后定义 objAppdelegate.window .... 它的工作
  • 然后尝试添加 [view commitAnimation] 的确切行,其他代码在 addsubview 行的上方尝试.....
猜你喜欢
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-16
相关资源
最近更新 更多