我目前有一个按钮:
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的视图,该视图具有日期选择器和工具栏。
答案 0 :(得分:3)
正如erhnby所说,你可以使用一个标签 - 这是一个很好的方法,但我总是尽量避免在我可以的情况下循环浏览视图的子视图。就个人而言,我会让视图你要删除一个实例变量,当你想要删除它时,你可以直接调用它删除...只是做了一个简单的例子:
.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
结果开始:
按按钮......
(抱歉,没有意识到白色的白色会很难看到)
答案 1 :(得分:-1)
为此设置标记将删除视图
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0 + 210)];
[containerView setTag:100];
找到它并removeFromSuperView
for (UIView* view in self.View3.subviews) {
if ([view isKindOfClass:[UIView class]] && view.tag == 100) {
[view removeFromSuperview];
}
}