开发者

How to update or reset a UIView with a custom subview?

开发者 https://www.devze.com 2023-01-14 08:29 出处:网络
I have a UIView in a UIViewController to which I add a custom subview in the viewDidAppear method. MyView *myView = [[MyView alloc] initWithLabelText:text];

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 vie开发者_StackOverflow中文版w 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 ;)


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.


The followings thing can ben considered.

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

0

精彩评论

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