I have two UIViewControllers in a storyboard (with a NavigationController).
I’m accessing the camera and taking a picture in ViewController1. I want to switch to ViewController2 and display the captured picture in a UIImageView.
ViewController2 H file
@interface ViewController2 : UIViewController
{
-IBOutlet UIImageView *matchImage;
}
-(IBAction)restart:(id)sender;
@property (retain, nonatomic) UIImageView *matchImage;
@end
ViewController1 M file
After my code to take the picture I attempt to switch views using the following syntax
ViewController2 *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
[self.navigationController pushViewController:vc animated:YES];
[vc.matchImage setImage:tempImage];
The first time I take a picture this works perfectly fine and I see the image in ViewController2.
However I have a back button on ViewController2 with the following syntax
ViewController2 M file
-(IBAction)restart:(id)sender
{
ViewController1 *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
[self.navigationController pushViewController:vc animated:YES];
}
This successfully returns to ViewController1 and I’m able to take a picture again and repeat the process. But after about 3-4 times repeating the process I get Received Memory Warning and the app crashes. Running the Analyzer in Xcode yields no issues.
If I comment out the following line
[vc.matchImage setImage:tempImage];
Then I can perform endless captures (over 50 with no issues) but of course ViewController2 does not show an image (it is blank). So this led me to believe I need to release matchImage (especially because it is a property with a retain attribute). But this didn’t seem to help. Any help would be greatly appreciated. I’m still learning about memory management.
UPDATE
Here is a bit more info, after 3 iterations of back and forth switching between the views this is the debugger output. I put in NSLog statements in the two ViewController viewDidUnload and didReceiveMemoryWarning methods. So when the Received memory warning occurs it appears 3 “instances” of the 2 viewcontrollers are being unloaded. So it appears that for each iteration of switching between the viewcontrollers I’m getting a new copy. Is this what you would expect?
2012-10-22 08:23:32.008 TestApp[787:707] Received memory warning.
2012-10-22 08:23:32.015 TestApp[787:707] viewDidUnload ViewController1: 0xfd52250
2012-10-22 08:23:32.017 TestApp[787:707] didReceiveMemoryWarning ViewController1: 0xfd52250
2012-10-22 08:23:32.023 TestApp[787:707] viewDidUnload ViewController2
2012-10-22 08:23:32.025 TestApp[787:707] didReceiveMemoryWarning ViewController2
2012-10-22 08:23:32.028 TestApp[787:707] viewDidUnload ViewController1: 0x16dc30
2012-10-22 08:23:32.030 TestApp[787:707] didReceiveMemoryWarning ViewController1: 0x16dc30
2012-10-22 08:23:32.033 TestApp[787:707] viewDidUnload ViewController2
2012-10-22 08:23:32.037 TestApp[787:707] didReceiveMemoryWarning ViewController2
2012-10-22 08:23:32.040 TestApp[787:707] viewDidUnload ViewController1: 0x171de0
2012-10-22 08:23:32.042 TestApp[787:707] didReceiveMemoryWarning ViewController1: 0x171de0
2012-10-22 08:23:32.044 TestApp[787:707] didReceiveMemoryWarning ViewController2
2012-10-22 08:23:32.046 TestApp[787:707] didReceiveMemoryWarning ViewController1: 0xfd87580
What you’re doing in your
restartmethod is not going back to your previous controller but adding another copy of the first controller type to the navigation stack. Try telling the navigation controller topopViewControllerAnimated:instead.