开发者

How to Unload a sub-view?

I've got a UITableView which loads a view when the accessory button is tapped:

MyView *newView = [[MyView alloc]  ...];
[self.navigationController pushViewController:newView];

Inside the view, when the user performs a certain action, I return back to the UITableView by doing this:

[self.navigationController popViewControllerAnimated:YES];

This works, but the next time the view is accessed by tapping the accessory button, it retains some of the values that it contained the first time the view used.

Is there a proper way to actually "completely unload" the view so that it's fresh the next time it is di开发者_如何学编程splayed?


You should autorelease the newView (which I assume is actually a View Controller, since you're pushing it to a navigation stack).

As it currently stands your view controller is never being deallocated and is being leaked:

  • It is allocated (retain count 1)
  • Pushed into a navigation stack which retains the view controller (retain count 2)
  • It is then popped from the stack, which releases it (retain count 1)
  • It is never released again after that

As for why you're getting previous values, you must also be pushing the same instance rather than creating a new one each time you push it to the stack - I can't tell since you haven't posted all of the relevant code.


You need to release any retained subviews of the main view on viewDidUnload...

For instance if you have retained UI elements by declaring them as outlets in IB, set them to nil before releasing them on dealloc.

Here's an example:

@interface ...

{
   IBOutlet UIButton *button;
   IBOutlet UITextLabel *label;
   IBOutlet UITableView *tableview;
}

@property (nonatomic, retain) UIButton *button;
...

Then you should set these to nil on viewDidUnload

- (void)viewDidUnload {
    self.button = nil;
    self.textlabel = nil;
    self.tableview = nil;
    ...
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜