0

I am using the following method to switch views:

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

 -(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.

enter image description here

Please help!

iPhoneNoob
  • 173
  • 1
  • 6
  • 19

3 Answers3

1

EXC_BAD_ACCESS usually appear when you deal with a release object (Zombies) .. go to Product > Edit Scheme > in the environment variables area add NSZombieEnabled with value yes .. this will show you the zombie objects in the console.

Malek_Jundi
  • 6,140
  • 2
  • 27
  • 36
  • I get now this error: **-[Animal performSelector:withObject:withObject:]: message sent to deallocated instance** I guess Caleb is right and something is deallocated... – iPhoneNoob Feb 14 '12 at 23:08
  • ok try to put this code in the animation block `[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];` and in this selector remove your view. – Malek_Jundi Feb 15 '12 at 08:11
1

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Hi Caleb, I did what Malek told me and now I'm getting this error: **-[Animal performSelector:withObject:withObject:]: message sent to deallocated instance** I am not sure how to do what you propose. Can u be more specific pls. Btw I think u were right about the deallocation... – iPhoneNoob Feb 14 '12 at 23:01
0

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. So I'll give both of you points for helping me! I appreciate it!

Community
  • 1
  • 1
iPhoneNoob
  • 173
  • 1
  • 6
  • 19