您好我是ios的初学者,我正在尝试以编程方式在我的父类上添加多个子视图,他们正在添加正常(这里我从后台类加载子视图)
但是当我在父视图类中添加第二个子视图时,我想删除我父视图类中的第一个子视图但是它不是删除我的代码,请在下面帮我一个
#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];
}
}
当我点击下面的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];
}
答案 0 :(得分:0)
试试这个
[firstview removeFromSuperview]
[secondview removeFromSuperview]
答案 1 :(得分:0)
请立即尝试
-(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];
}
}