简体   繁体   English

将视图切换回无法在Xcode中工作

[英]Switching views back not working in xcode

I am using the following method to switch views: 我正在使用以下方法切换视图:

FirstView.m 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;
}

Method on Second View Animal.m Second View 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];

}

I used this code several times and it always worked without any problems, until now. 我多次使用此代码,直到现在,它始终可以正常工作。 My app is crushing every time when I want to switch back. 每当我想返回时,我的应用程序都崩溃了。 So the method on the Animal view crashes. 因此,“ 动物”视图上的方法崩溃了。 The weird thing is that I get an odd error. 奇怪的是我得到了一个奇怪的错误。 I got a screenshot of that. 我有一个截图。

在此处输入图片说明

Please help! 请帮忙!

EXC_BAD_ACCESS通常在处理发行对象(僵尸)时出现。.转到产品>编辑方案>在环境变量区域中,将NSZombieEnabled值添加为yes ..这将向您显示控制台中的僵尸对象。

I think what's happening here is that you're removing the view from its superview before the animation is complete. 我认为这里发生的是,您要在动画完成之前从其超级视图中删除该视图。 If your view controller hasn't retained the view that's removed, it'll be released and probably deallocated as soon as it's removed. 如果您的视图控制器尚未保留已删除的视图,则它将被释放,并可能在删除后立即释放。 The animation will cause the bad access exception you're getting because it's trying to access an object that's no longer there. 动画将导致您获取错误的访问异常,因为它试图访问的对象不再存在。

To fix, avoid removing the view from the superview until after the animation has completed. 要进行修复,请避免在动画完成之前从超级视图中删除视图。

Thanks for your help but I found another way to handle that error. 感谢您的帮助,但我找到了另一种方法来处理该错误。 You were right. 你是对的。 The previous view was deallocated before returning to him. 在返回他之前,先前的视图已被释放。 That was due to ARC(Automatic reference counting) so I turned it off using this article . 那是由于ARC(自动引用计数)造成的,所以我在本文中将其关闭了。 So I'll give both of you points for helping me! 所以我会给你们两个帮助我的积分! I appreciate it! 我很感激!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM