开发者

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜