【发布时间】:2014-12-29 05:08:05
【问题描述】:
我正在开发一个需要根据从 Web 服务收到的 json 动态生成 UITextFields 的应用程序。实际上它是一个视图结构。它包含文本字段对象的坐标及其详细信息。我正在使用预定义数量的文本字段生成结构。
for (int i=0; i<[textFieldsArray count];i++) {
NSDictionary *textFieldDict = [textFieldsArray objectAtIndex:i];
switch (i+1) {
case 1:
textField1 = [[UITextField alloc]init];
textField1.tag = i+1;
textField1.text = [textFieldDict valueForKey:@"text"];
textField1.frame = CGRectMake(posx,posy,textWidth,textHeight);
[textField1 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventTouchDown];
[self.imageView addSubview:textField1];
break;
case 2:
textField2 = [[UITextField alloc]init];
textField2.tag = i+1;
textField2.text = [textFieldDict valueForKey:@"text"];
textField2.frame = CGRectMake(posx,posy,textWidth,textHeight);
[self.imageView addSubview:textField2];
[textField2 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventTouchDown];
break;
case 3:
textField3 = [[UITextField alloc]init];
textField3.tag = i+1;
textField3.text = [textFieldDict valueForKey:@"text"];
textField3.frame = CGRectMake(posx,posy,textWidth,textHeight);
[self.imageView addSubview:textField3];
[textField3 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventTouchDown];
break;
default:
break;
}
}
我想将其扩展为无限的文本字段。有没有办法动态创建文本字段。
【问题讨论】:
-
您正在动态创建文本字段。我认为您要问的更多是关于变量-您可以将文本字段引用存储在 NSMutableArray 中,而不是单个 UItextField 变量
-
@Paulw11 你能帮我动态生成文本字段数组吗?我正在努力动态命名文本字段。我听说我们不能在运行时动态生成变量名。
标签: ios objective-c dynamic programmatically-created