Toran Billups
Toran Billups

Reputation: 27399

How can I reload view controllers from the appDelegate each time I push one on the stack?

Currently my application has a single navigation screen that allows the user to select other views. When clicked the navController simply does a push of the specific view controller in question. This works great if I never come back to the same view again expecting it to be reloaded.

In the code below I push a view controller when requested.

- (void)optionClicked:(NSString *)optionName
{
  if ([@"First" isEqualToString:optionName]) {
    [navController pushViewController:firstController animated:YES];
  } else if ([@"Next" isEqualToString:optionName]) {
    [navController pushViewController:nextController animated:YES];
  }
}

When done with a view I simply pop it from the stack. But the next time a user selects this same option from the menu it's not loaded "clean" and this is where my question comes in. How can I load a view controller fresh each time it's pushed on the stack?

Upvotes: 0

Views: 1987

Answers (1)

mackross
mackross

Reputation: 2234

You have to reinitialize the viewcontroller.

  if ([@"First" isEqualToString:optionName]) {
    if (firstController) 
    { [firstController release]; } // assuming you've got a retain on it.
    firstController = [[MyViewControllerSubclass alloc] init];
    [navController pushViewController:firstController animated:YES];
  } 

In this situation I would suggest using a property with retain on it. @property (nonatomic, retain) MyViewControllerSubclass *firstController;

that way you can use self.firstController = [[[MyViewControllerSubclass alloc] init] autorelease]; and the memory management is mostly done for you. (Although you still have to release in dealloc.)

Upvotes: 2

Related Questions