【问题标题】:Switching views back not working in xcode将视图切换回在 xcode 中不起作用
【发布时间】:2012-02-14 22:32:12
【问题描述】:

我正在使用以下方法切换视图:

FirstView.m

-(IBAction)showAnimal:(UIButton *)sender{
    NSString *digit = [NSString stringWithFormat:@"%d",[sender tag]];
     //NSString *digit = [[sender titleLabel] text];


    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.indexOfImage = digit;

    Animal *myAnimal = [[Animal alloc] 
                             initWithNibName:@"Animal" bundle:nil];

    [UIView beginAnimations:@"flipView" context:nil];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft  
                           forView:self.view cache:YES];

    [UIView commitAnimations];

    //[self presentModalViewController:myAnimal animated:YES];


    myAnimal.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);


    [self.view addSubview:myAnimal.view];

    self.view.bounds = myAnimal.view.bounds;
}

第二视图的方法 Animal.m

 -(IBAction)backToFront:(id)sender{

[UIView beginAnimations:@"flipView" context:nil];
[UIView setAnimationDuration:1.2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
                      forView:self.view.superview cache:YES];


[self.view removeFromSuperview];
[UIView commitAnimations];

}

我多次使用此代码,直到现在它始终可以正常工作。 每次我想切换回来时,我的应用程序都会崩溃。所以 Animal 视图上的方法崩溃了。奇怪的是我得到了一个奇怪的错误。我有一个截图。

请帮忙!

【问题讨论】:

    标签: iphone ios xcode uiviewcontroller


    【解决方案1】:

    EXC_BAD_ACCESS 通常在您处理释放对象(Zombies)时出现 .. 转到 Product > Edit Scheme > 在环境变量区域添加 NSZombieEnabled 值为 yes .. 这将在控制台中显示僵尸对象。

    【讨论】:

    • 我现在得到这个错误:-[Animal performSelector:withObject:withObject:]: message sent to deallocated instance 我猜 Caleb 是对的,有些东西被释放了...
    • ok 尝试将此代码放入动画块[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 并在此选择器中删除您的视图。
    【解决方案2】:

    我认为这里发生的情况是您在动画完成之前从其父视图中删除了视图。如果您的视图控制器没有保留已删除的视图,它将被释放并且可能会在它被删除后立即释放。该动画将导致您遇到错误的访问异常,因为它正在尝试访问不再存在的对象。

    要解决此问题,请避免在动画完成之前从超级视图中删除视图。

    【讨论】:

    • 嗨 Caleb,我按照 Malek 告诉我的做了,现在我收到了这个错误:-[Animal performSelector:withObject:withObject:]: message sent to deallocated instance 我我不知道如何做你的建议。你能更具体吗?顺便说一句,我认为你对解除分配的看法是正确的......
    【解决方案3】:

    感谢您的帮助,但我找到了另一种处理该错误的方法。你是对的。之前的视图在返回给他之前被解除分配。这是由于 ARC(自动引用计数),所以我使用 this article 将其关闭。所以我会给你们两个点来帮助我!我很感激!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多