如何删除 Objective-C 中的子视图?

发布于 2024-09-05 02:55:55 字数 815 浏览 6 评论 0原文

我已以编程方式将 UIButton 和 UITextView 作为子视图添加到我的视图中。

notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notesDescriptionView.backgroundColor = [UIColor redColor];
[self.view addSubview:notesDescriptionView];

textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)]; 
[self.view addSubview:textView]; 
printf("\n description  button \n");

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button
  addTarget:self action:@selector(cancel:)
  forControlEvents:UIControlEventTouchDown];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
[self.view addSubview:button];

单击按钮时我需要删除所有子视图。

我已经尝试过:

[self.view removeFromSuperView]

但它不起作用。

I have added UIButton and UITextView as subviews to my view programmatically.

notesDescriptionView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,460)];
notesDescriptionView.backgroundColor = [UIColor redColor];
[self.view addSubview:notesDescriptionView];

textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,320,420)]; 
[self.view addSubview:textView]; 
printf("\n description  button \n");

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button
  addTarget:self action:@selector(cancel:)
  forControlEvents:UIControlEventTouchDown];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 420.0, 160.0, 40.0);
[self.view addSubview:button];

I need to remove all subviews when the button is clicked.

I have tried:

[self.view removeFromSuperView]

but it's not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

ゝ杯具 2024-09-12 02:55:55

要删除添加到视图中的所有子视图,

请使用以下代码

for (UIView *view in [self.view subviews]) 
{
    [view removeFromSuperview];
}

to remove all the subviews you added to the view

use the following code

for (UIView *view in [self.view subviews]) 
{
    [view removeFromSuperview];
}
街角卖回忆 2024-09-12 02:55:55

我假设您正在从与上述代码片段相同的类中的方法调用 [self.view removeFromSuperView]

在这种情况下,[self.view removeFromSuperView] 会从其自己的超级视图中删除 self.view,但 self 是您希望从中删除子视图的对象。如果您想删除对象的所有子视图,则需要这样做:

[notesDescriptionView removeFromSuperview];
[button.view removeFromSuperview];
[textView removeFromSuperview];

也许您希望将这些子视图存储在 NSArray 中,并调用 removeFromSuperview 循环遍历该数组code> 该数组中的每个元素。

I assume you're calling [self.view removeFromSuperView] from a method in the same class as the above snippet.

In that case [self.view removeFromSuperView] removes self.view from its own superview, but self is the object from whose view you wish to remove subviews. If you want to remove all the subviews of the object, you need to do this instead:

[notesDescriptionView removeFromSuperview];
[button.view removeFromSuperview];
[textView removeFromSuperview];

Perhaps you'd want to store those subviews in an NSArray and loop over that array invoking removeFromSuperview on each element in that array.

站稳脚跟 2024-09-12 02:55:55

我一直很惊讶 Objective-C API 没有一个简单的方法来从 UIView 中删除所有子视图。 (Flash API 确实如此,而且您最终会非常需要它。)

无论如何,这是我用于此目的的小辅助方法:

- (void)removeAllSubviewsFromUIView:(UIView *)parentView
{
  for (id child in [parentView subviews])
  {
    if ([child isMemberOfClass:[UIView class]])
    {
      [child removeFromSuperview];
    }
  }
}

编辑:刚刚在这里找到了一个更优雅的解决方案: 从 self.view 中删除所有子视图的最佳方法是什么?

现在使用它如下:

  // Make sure the background and foreground views are empty:
  [self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  [self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

我更喜欢它。

I've always been surprised that the Objective-C API doesn't have a simple method for removing all sub views from a UIView. (The Flash API does, and you end up needing it quite a bit.)

Anyway, this is the little helper method that I use for that:

- (void)removeAllSubviewsFromUIView:(UIView *)parentView
{
  for (id child in [parentView subviews])
  {
    if ([child isMemberOfClass:[UIView class]])
    {
      [child removeFromSuperview];
    }
  }
}

EDIT: just found a more elegant solution here: What is the best way to remove all subviews from you self.view?

Am using that now as follows:

  // Make sure the background and foreground views are empty:
  [self.backgroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  [self.foregroundContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

I like that better.

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