I am programmatically creating a view in the loadView method of my view controller. As follows:
-(void)loadView {
HPSFormView* viewForThisController = [ [ HPSFormView alloc ] initWithFrame:CGRectZero ] ; viewForThisController.controller = self; // Set a reference back to this controller so the View knows who to delegate stuff to when > adding controls self.view = viewForThisController; // Set the view for this controller to be the main menu view}
I follow this technique for three different pairs of view controllers and views. I am pushing the view controllers onto a UINavigationController stack. Each view contains a button that uses push. As follows:
-(void)buttonTapped:(id)sender {
HPSFormController* formVC = [ [ HPSFormController alloc ] initWithNibName:nil bundle:nil ];
[(UINavigationController*) self.view.window.rootViewController pushViewController:formVC animated:YES];
}
I do not have any code in viewDidLoad – indeed, it is entirely absent from my view controllers.
Everything works fine, but when I do a Simulate Memory Warning within the iOS simulator then the app crashes. I believe the problem lies with the way that I am defining my view as a ‘local’ variable within loadView.
Could anyone tell me whether my loadView looks ok, or whether it is necessary to define loadView differently to avoid invalid references etc.
Many thanks.
The problem was that the view was holding a reference to the controller. When the view was unloaded then the controller reference went out of scope, so boom!