【问题标题】:How to remove a programmatically created subview by pressing a button within the subview如何通过按下子视图中的按钮来删除以编程方式创建的子视图
【发布时间】:2014-04-18 20:55:45
【问题描述】:

我目前有一个条形按钮:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneDate:)];

它调用以下动作:

- (IBAction)doneDate:(id)sender{
[self removeDateView]
}

调用如下方法:

- (void)removeDateView{

NSLog(@"subviews of view3.view: %@",self.View3.subviews);
[self.View3.subviews. makeObjectsPerformSelector: @selector(removeFromSuperview)];

}

我要删除的子视图是

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0 + 210)];

目前它只是删除了该视图中的所有内容,我似乎无法删除名为 containerView 的视图,该视图具有日期选择器和工具栏。

【问题讨论】:

  • 为包含日期选择器和工具栏的视图设置标签,然后使用带有您指定标签的 viewWithTag 方法获取此视图。然后将其从超级视图中删除。

标签: ios


【解决方案1】:

正如 erhnby 所说,您可以使用标签 - 这是一种很好的方法,但我总是尽量避免循环浏览视图的子视图。就我个人而言,我会制作你要删除一个实例变量的视图,当你想删除它时,你可以直接在它上面调用 remove... 只是做了一个简单的例子:

.h 文件:

#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController {
    UIView *_containerView;
}

@end

.m 文件:

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

- (id)init {
    self = [super init];

    // create the bar button and set it as the right bar button on the navigation bar
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(removeDoneDate)];

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // create the container view and add it as a subview
    _containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 100, 100)];
    _containerView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_containerView];

}

- (void)removeDoneDate {
    // remove it
    [_containerView removeFromSuperview];
}

@end

结果在此开始:

按下按钮...

(抱歉,没想到白底白字这么难看)

【讨论】:

  • 啊,我明白了,你已经很容易理解了,谢谢。希望我能正确实施。
  • 如果您需要任何帮助,请告诉我!
  • 子视图现在正在 NSLog 中注册,因为我已经添加了 [self.view addSubview:_containerView];但它在 viewDidLoad 中加载,而不是在按下文本字段的输入视图时作为子视图加载
  • 你是什么 NSLogging?
  • 您将 _containerView 添加到 self.'view',但您正在打印 self.'View3' 的子视图。会这样吗?
【解决方案2】:

为此设置标签将删除视图

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0 + 210)];
[containerView setTag:100];

找到它并删除FromSuperView

for (UIView*  view in self.View3.subviews) {
        if ([view isKindOfClass:[UIView class]] && view.tag == 100) {
            [view removeFromSuperview];
        }
    }

【讨论】:

  • 它似乎不起作用,可以按下完成按钮但没有任何反应。 NSLog(@"view3.view 的子视图:%@",self.View3.subviews);我认为问题在于 .subviews 只返回“即时”视图,我假设这些视图已经在我的故事板上。我认为这是问题所在,因为我的视图是以编程方式创建的,而不是即时的?
  • self.view3 有子视图?
  • NSLog 说 View3.subviews 返回 - 和一个
  • 它们对于我故事板上的对象都是正确的,但对于我以编程方式添加的视图却不正确
  • 使用viewWithTag:有什么问题。为什么要遍历子视图?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多