我在数组中添加 UIViews。单击按钮后,我将它们添加到另一个视图。完美运行。
问题是,当我在数组中多次添加视图时。视图只设置一次。
这是我的代码。
-(IBAction)buttonClick:(id)sender {
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
view1.backgroundColor = [UIColor redColor];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
[view2 setBackgroundColor:[UIColor orangeColor]];
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
[view3 setBackgroundColor:[UIColor greenColor]];
UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
[view4 setBackgroundColor:[UIColor blueColor]];
NSArray *views = [NSArray arrayWithObjects:view2, view2, view1, view4, view3, nil];
[self setViews:views];
}
-(void) setViews:(NSArray*)views {
[testView1 addSubview: [views objectAtIndex:0]];
[testView2 addSubview: [views objectAtIndex:1]];
[testView3 addSubview: [views objectAtIndex:2]];
[testView4 addSubview: [views objectAtIndex:3]];
[testView5 addSubview: [views objectAtIndex:4]];
}
输出是,
对于NSArray *views = [NSArray arrayWithObjects:view1, view2, view4, view4, view3, nil];
,输出为,
对于NSArray *views = [NSArray arrayWithObjects:view1, view2, view3, view4, view4, nil];
,输出为,
我说得更清楚了。对于[NSArray arrayWithObjects:view1, view1, view1, view1, view1, nil];
, testView1,2,3,4 为空并view1
添加到testView5
.
无论添加多少次视图,如何才能带来完美的输出?
我想听听你的解释..!