我在UIView中添加多个UITextField时出现问题,它们被正确添加但是用户只能编辑它们中的第一个(触摸它以便键盘出现),我已将委托设置为self,并且已实现方法textFieldDidBeginEditing。可悲的是,只有第一个反应触发了这种方法,其他似乎是“不可触碰的”。
你以前见过这个吗?
for (int i = 0;i<N;i++){
CGRect textFieldNewFrame = CGRectMake(200,60 + 34*i,72,30);
fields[i] = [[UITextField alloc] initWithFrame:textFieldNewFrame];
[fields[i] setTag:i];
[fields[i] setText:@"0"];
[fields[i] setFont:appFont];
[fields[i] setTextColor:appColor];
[fields[i] setBackground:[UIImage imageNamed:@"text_field_off.png"]];
[fields[i] setTextAlignment:UITextAlignmentCenter];
[fields[i] setKeyboardType:UIKeyboardTypeDecimalPad];
[fields[i] setDelegate:self];
[fields[i] setUserInteractionEnabled:TRUE];
[listView addSubview:fields[i]];
verticalOffset += 34;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField;{
NSLog(@"TextField touched");
}
由于
答案 0 :(得分:0)
我会这样做,它只是一个面向对象的答案,应该工作,已经为我工作了很多次。
NSMutableArray *fields=[[NSMutableArray alloc] init];
for (int i = 0;i<N;i++){
CGRect textFieldNewFrame = CGRectMake(200,60 + 34*i,72,30);
[fields addObject:[[UITextField alloc] initWithFrame:textFieldNewFrame]];
[[fields objectAtIndex:i] setTag:i];
[[fields objectAtIndex:i] setText:@"0"];
[[fields objectAtIndex:i] setFont:appFont];
[[fields objectAtIndex:i] setTextColor:appColor];
[[fields objectAtIndex:i] setBackground:[UIImage imageNamed:@"text_field_off.png"]];
[[fields objectAtIndex:i] setTextAlignment:UITextAlignmentCenter];
[[fields objectAtIndex:i] setKeyboardType:UIKeyboardTypeDecimalPad];
[[fields objectAtIndex:i] setDelegate:self];
[[fields objectAtIndex:i] setUserInteractionEnabled:TRUE];
[listView addSubview:[fields objectAtIndex:i]];
verticalOffset += 34; }