如何使用自定义子视图更新或重置 UIView?

发布于 2024-09-16 11:00:39 字数 689 浏览 11 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑依然 2024-09-23 11:00:39

不要每次都分配和初始化自定义子视图,仅在第一次调用 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.

沦落红尘 2024-09-23 11:00:39

可以考虑以下事情。

viewDidLoad -->分配并初始化您的子视图
viewDidAppear -->更新子视图
解除分配-->释放子视图。

The followings thing can ben considered.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文