以编程方式添加了多个UITextField,只能编辑一个

时间:2011-12-02 14:26:04

标签: iphone ios uitextfield interaction

我在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");
}

由于

1 个答案:

答案 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; }