开发者

what function should I implement to cleanup the view of a UINavigationController

I have A UINavigationController with a table view. As it's standard behaviour when an item in the list is selected I push a customViewController to the UINavigationController. The CustomView Appears and I see my back button in the title b开发者_开发问答ar.

My question is, when I hit the back button in the title bar to navigate back to my list what function do I implement to make sure that everything that was created in the customViewController is completely destroyed and removed from memory?

I tried putting my cleanup code in the viewdidunload method of the custom controller but that doesnt even get entered when I hit the back button.

(Also I wasnt really sure how to phrase this question so suggestions are welcome)


Apple explains everything very clearly in their documenation (with pretty pictures and everything!). Basically, when you show the view you use pushViewController:animated: and when you go back you use popViewControllerAnimated:.

Use something like this to go to the new screen:

- (IBAction)goSomewhereButtonPressed:(id)sender {
    SomewhereViewController *vc = [[SomewhereViewController alloc] initWithNibName:@"SomewhereView" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

When the BACK button is pressed, it will clean-up your screen. Apple recommends you use UINavigationControllerDelegate for additional setup & cleanup if needed.

Put the cleanup for the screen in its controller (SomewhereViewController).

    - (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    NSLog(@"Somewhere's viewDidUnload called");
}

- (void)dealloc
{
    [super dealloc];
    NSLog(@"Somewhere's dealloc called");
}


I always put my cleanup code in dealloc:

-(void)dealloc {
   // cleanup code here
   [super dealloc];
}


The allocated controllers inside a UINAvigationController will be removed automatically. If you need to let just one live, create the detail controller globally in your navigation controller instead every time you need to go to the detail view, so you will use always the same controller. You can clean it when the back button is pressed through the method viewDidDisappear.


The viewDidUnload method of UIViewController seems to be a good place to do memory cleanup, i.e. release all objects that can be easily recreated in viewDidLoad or later.

But it's not guaranteed that the view controller itself will be dealloc'ed. The UINavigationController may cache the object internally.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜