这样根据数据信息,动态创建了一堆UITextField,每一个UITextField都对应了一个UIButton。
for(i=0; i<[users count]; i++)
{
NSString *name = [users objectAtIndex:i];
UITextField *user = [[UITextField alloc] initWithFrame:CGRectMake(0.f, top, 200.f, 30.f)];
user.text = name;
user.returnKeyType = UIReturnKeyDone;
user.keyboardType = UIKeyboardTypeDefault;
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(220.f, top, 80.f, 30.f)];
[btn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
[self.scroller addSubview:user];
[self.scroller addSubview:btn];
top = top + 40;
}
现在要通过点击某个Button,修改对应的UITextField的值
之前都是把UITextField做成全局变量的,现在动态创建不能全局了,怎么办?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
创建时
for(i=0; i<[users count]; i++) { NSString *name = [users objectAtIndex:i]; UITextField *user = [[UITextField alloc] initWithFrame:CGRectMake(0.f, top, 200.f, 30.f)]; //注意这句 user.tag = i; //注意上面这句 user.text = name; user.returnKeyType = UIReturnKeyDone; user.keyboardType = UIKeyboardTypeDefault; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(220.f, top, 80.f, 30.f)]; [btn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside]; [self.scroller addSubview:user]; [self.scroller addSubview:btn]; top = top + 40; }获取时
另建议将user命名为userTextField