[英]How to remove sub views from parent view
Hi i am beginner in ios and i am trying to add multiple child views programmatically on my parent class they are adding fine(here i am loading child views from background class) 嗨,我是ios的初学者,我正在尝试以编程方式在我的父类上添加多个子视图,这些子视图正在添加(在这里,我正在从背景类加载子视图)
But when i add second child view on my parent view class i want to remove first child view on my parent view class but it is not removing my code is below please help me some one 但是,当我在父视图类上添加第二个子视图时,我想在父视图类上删除第一个子视图,但并未删除我的代码,请在下面帮助我
#import "MainView1.h"
@interface MainView1 ()
@end
@implementation MainView1
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)loadView1 :(UIView *)myview :(int)viewValue :(UIViewController*)VC
{
UIView * firstview;
UIView * secondview;
if (viewValue == 1) {
[firstview willRemoveSubview:myview];
[secondview willRemoveSubview:myview];
firstview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[firstview setBackgroundColor:[UIColor yellowColor]];
UIButton *addProject=[[UIButton alloc]init];
addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(100, 285, 100, 18);
addProject.backgroundColor = [UIColor redColor];
[addProject setTitle:@"Show View" forState:UIControlStateNormal];
[addProject addTarget:VC action:@selector(ProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[firstview addSubview:addProject];
[myview addSubview:firstview];
}
else {
[firstview willRemoveSubview:myview];
[secondview willRemoveSubview:myview];
secondview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 400)];
[secondview setBackgroundColor:[UIColor greenColor]];
UIButton *addProject1=[[UIButton alloc]init];
addProject1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject1.frame = CGRectMake(200, 285, 100, 18);
addProject1.backgroundColor = [UIColor redColor];
[addProject1 setTitle:@"Show View1" forState:UIControlStateNormal];
[addProject1 addTarget:VC action:@selector(ProjectPressed123:) forControlEvents:UIControlEventTouchUpInside];
[secondview addSubview:addProject1];
[myview addSubview:secondview];
}
}
when i clicked on below ProjectPressed button action i want to add second child view on my parent view class, It's adding fine but first child view is not removed from parent view class 当我单击下面的ProjectPressed按钮操作时,我想在父视图类上添加第二个子视图,这很好,但是第一个子视图并未从父视图类中删除
#import "ViewController.h"
#import "MainView1.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MainView1 * m1 = [[MainView1 alloc]init];
[m1 loadView1:self.view :1 :self];
}
- (void)ProjectPressed:(id)sender
{
MainView1 * m1 = [[MainView1 alloc]init];
[m1 loadView1:self.view :2 :self];
}
Try this 尝试这个
[firstview removeFromSuperview]
[secondview removeFromSuperview]
Please try now 请现在尝试
-(void)loadView1 :(UIView *)myview :(int)viewValue :(UIViewController*)VC
{
UIView * firstview;
UIView * secondview;
for (UIView *view in [myview subviews])
{
[view removeFromSuperview];
}
if (viewValue == 1) {
firstview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[firstview setBackgroundColor:[UIColor yellowColor]];
UIButton *addProject=[[UIButton alloc]init];
addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(100, 285, 100, 18);
addProject.backgroundColor = [UIColor redColor];
[addProject setTitle:@"Show View" forState:UIControlStateNormal];
[addProject addTarget:VC action:@selector(ProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[firstview addSubview:addProject];
[myview addSubview:firstview];
}
else {
secondview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 400)];
[secondview setBackgroundColor:[UIColor greenColor]];
UIButton *addProject1=[[UIButton alloc]init];
addProject1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject1.frame = CGRectMake(200, 285, 100, 18);
addProject1.backgroundColor = [UIColor redColor];
[addProject1 setTitle:@"Show View1" forState:UIControlStateNormal];
[addProject1 addTarget:VC action:@selector(ProjectPressed123:) forControlEvents:UIControlEventTouchUpInside];
[secondview addSubview:addProject1];
[myview addSubview:secondview];
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.