如何使用自定义子视图更新或重置 UIView?
我在 UIViewController 中有一个 UIView,我在 viewDidAppear 方法中添加了一个自定义子视图。
MyView *myView = [[MyView alloc] initWithLabelText:text];
[self.view addSubview:myView];
[myView release];
text
变量是在 myView
中的标签上使用的字符串。每次您返回当前视图时,此文本
都会发生变化。但似乎 viewDidAppear 不会重新加载视图 - 它而是在旧视图上加载新视图 - 所以我有两个相互重叠的标签。
我尝试使用 viewWillAppear 但没有任何区别。我还尝试使用 [self.view setNeedsDisplay]
- 没有帮助。我还尝试将 myView
设为实例变量,但也没有帮助。
当我将视图声明为实例变量时,有效的方法是显式删除视图:
- (void)viewDidDisappear:(BOOL)animated
{
[_myView removeFromSuperview];
}
尽管有此解决方法,但我想在返回视图时简单地重置视图。有人知道该怎么做吗?我将不胜感激;)
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 ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要每次都分配和初始化自定义子视图,仅在第一次调用 viewDidAppear 时才分配和初始化。然后将其保留在财产中以供后续使用。
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.
可以考虑以下事情。
viewDidLoad -->分配并初始化您的子视图
viewDidAppear -->更新子视图
解除分配-->释放子视图。
The followings thing can ben considered.
viewDidLoad --> alloc and init your sub views
viewDidAppear --> update sub views
dealloc --> release sub views.