views:

28

answers:

2

Hey everyone,

I have a UIView in a UIViewController to which I add a custom subview in the viewDidAppear method.

MyView *myView = [[MyView alloc] initWithLabelText:text];
[self.view addSubview:myView];
[myView release];

The text variable is a string used on a label in myView. This text changes every time you come back to the current view. But it seems that viewDidAppear does not reload the view - it rather loads a new view over the old one - so I have two labels over each other.

I tried to use viewWillAppear but it doesn't make any difference. I also tried to use [self.view setNeedsDisplay] - doesn't help. I also tried to make myView an instance variable, but it also didn't help.

What worked was to remove the view explicitly, when I declared it as an instance variable:

- (void)viewDidDisappear:(BOOL)animated
{
    [_myView removeFromSuperview];
}

Although there is this workaround I would like to simply reset the view when getting back to it. Does anybody know how to do that? I would appreciate it ;)

A: 

Don't alloc and init the custom subview every time, only the first time viewDidAppear is called. Then retain it in a property for subsequent use.

hotpaw2
Thank you. But then it has to be an instance variable again, doesn't it?
marikaner
Not necessarily, but an instance variable that's also a property is a good place, because that will help manage the retain count.(And make sure not to "cover" your instance variable with a local variable of the same name, or use an instance variable in another object by mistake.)
hotpaw2
A: 

The followings thing can ben considered.

viewDidLoad --> alloc and init your sub views viewDidAppear --> update sub views dealloc --> release sub views.

alones
Thank you. Yes, this is what I have done. But here also I have to use instance variables instead of local variables. I thought there should be a way to "reload" the whole view.
marikaner