1、申明,再@implementation中申明私有变量
UITextField *roleField[5]; // C语言写法 申明roleField数组,数组里存放5个UITextField对象
2、创建要使用的UITextField对象,通过for循环进行创建,参考:
roleField[0] = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
roleField[1] = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
roleField[2] = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
roleField[3] = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
roleField[4] = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
//UITextField对象的属性设置参考
roleField[0].text = @"test";//前提是对象存在才能设置哦
或者
//注意:如果使用for创建,循环次数i必须要和C申明的时候保持一致,否则会导致问题
for (int i=0; i<5; i++) {
roleField[i] = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
roleField[i].delegate = self;
roleField[i].tag = i;
roleField[i].returnKeyType = UIReturnKeyDone;
roleField[i].placeholder = array[i];
roleField[i].backgroundColor = [UIColor lightGrayColor];
[self addSubview:roleField[i]];
}
如果要单独设置指定的UITextField,通过roleField[0].text = @"test";这种方式即可
网友评论