我目前有一个条形按钮:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle"Done" style:UIBarButtonItemStyleBordered target:self actionselector(doneDate];
它调用以下 Action :
- (IBAction)doneDateid)sender{
[self removeDateView]
}
调用如下方法:
- (void)removeDateView{
NSLog(@"subviews of view3.view: %@",self.View3.subviews);
[self.View3.subviews. makeObjectsPerformSelector: @selector(removeFromSuperview)];
}
我要删除的 subview 是
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0 + 210)];
目前它只是删除了该 View 中的所有内容,我似乎无法删除名为 containerView 的 View ,该 View 具有日期选择器和工具栏。
Best Answer-推荐答案 strong>
正如 erhnby 所说,您可以使用标签 - 这是一种很好的方法,但我总是尽量避免循环浏览 View 的 subview 。就我个人而言,我会制作你要删除实例变量的 View ,当你想删除它时,你可以直接在它上面调用 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 actionselector(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
结果在此开始:
按下按钮...
(抱歉,没想到白底白字这么难看)
关于ios - 如何通过按下 subview 中的按钮来删除以编程方式创建的 subview ,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/23162201/
|