在用户按钮的时候创建一个元素,比如UIView。
NSMutableString *myVar = [NSMutableString stringWithFormat:@"_view%i", num];
UIView * newView = [self valueForKey:myVar];
但是在.h文件没有完全添加:
UIView * _view1;
UIView * _view2;
...
你可以在父视图中动态创建很多个subView. 如果想在创建好后操作这个subview,你可以给每个subview设置tag的值。这样就可以通过下面的代码来找到相应的subview.而不用每个UIView都需要在头文件中声明
/////创建5个subview
for (int i=0; i<5 ; i++) {
UIView *view=[[UIView alloc] init];
view.tag=i;
[self.view addSubview:view];
}
////根据tag来找出第二个创建的view
UIView *secondView=(UIView *)[self.view viewWithTag:1];
- (IBAction)userTouchedButton:(id)sender{
for (int i = 0; i < 100; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[view setBackgroundColor:[UIColor redColor]];//to distinguish theseViews from other views
[view setTag:i];//to identified it later
[_array insertObject:view atIndex:i];// globleMutble array
[self.view addSubview:view];
}
}