如何在循环内创建动态文本字段并使其全部可编辑模式?

时间:2015-01-19 04:34:45

标签: ios iphone xcode

我一直面临着在循环中以编程方式创建uitextfield并将它们全部置于可编辑模式的问题?

我在循环中成功创建了文本字段,但问题是这样。最后一个,即最后一个文本字段处于可编辑模式。

EX: 假设一个数组(myarray)有5个值。

-(void)createDynamic_textfld{

    UITextField *roomratetxt;

    UIScrollView *scrlsummery;

    NSMutableArray *myarray = [NSMutableArray arrayWithObjects:@[@"one",@"two",@"three",@"four",@"five"], nil];

    for (int i = 0; [myarray count]; i ++){

        roomratetxt = [[UITextField alloc]init];

        roomratetxt.frame = CGRectMake(200, 0, 90, 20);

        roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lighttextcolor.png"]];

        roomratetxt.backgroundColor = [UIColor clearColor];

        roomratetxt.font = [UIFont systemFontOfSize:12];

        roomratetxt.text = [NSString stringWithFormat:@"%@",[myarray objectAtIndex:i]];

        roomratetxt.textAlignment = NSTextAlignmentRight;

        [roomratetxt setTag:i];

        [roomratetxt setDelegate:(id)self];

        [scrlsummery addSubview:roomratetxt];

    }

}

我希望以可编辑模式制作所有5个文本字段。但截至目前,最后一个文本字段仅处于编辑模式,其余均为非可编辑模式。

任何人都可以向我提出一些想法吗?如何实现这一目标

提前致谢。 Ajeet Kumar

3 个答案:

答案 0 :(得分:0)

首先,你需要提供不同的框架,你可以在不同的位置。您的上述代码看起来是正确的,并且必须处于可编辑模式。我在我的项目上执行下面的片段,它就像一个魅力。我假设您正在使用Scrollview并在其中添加了所有文本字段。唯一可以确保你没有从

中禁用它们
  
      
  • (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; //返回NO以禁止编辑。
  •   

  
      
  • (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)范围   replacementString:(NSString *)string; //返回NO不变化   文本
  •   

for(int i = 0; [myarray count]; i ++){

roomratetxt = [[UITextField alloc]init];

roomratetxt.frame = CGRectMake(200, i*20, 90, 20);

roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lighttextcolor.png"]];

roomratetxt.backgroundColor = [UIColor clearColor];

roomratetxt.font = [UIFont systemFontOfSize:12];

roomratetxt.text = [NSString stringWithFormat:@"%@",[myarray objectAtIndex:i]];

roomratetxt.textAlignment = NSTextAlignmentRight;

[roomratetxt setTag:i];

[roomratetxt setDelegate:(id)self];

[scrlsummery addSubview:roomratetxt];

}

答案 1 :(得分:0)

-(void)createDynamic_textfld{

    UITextField *roomratetxt;

    UIScrollView *scrlsummery;

    NSMutableArray *myarray = [NSMutableArray arrayWithObjects:@[@"one",@"two",@"three",@"four",@"five"], nil];

    for (int i = 0; [myarray count]; i ++){

        roomratetxt = [[UITextField alloc]init];

        roomratetxt.frame = CGRectMake(200, i*0, 90, 20);

        roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lighttextcolor.png"]];

        roomratetxt.backgroundColor = [UIColor clearColor];

        roomratetxt.font = [UIFont systemFontOfSize:12];

        roomratetxt.text = [NSString stringWithFormat:@"%@",[myarray objectAtIndex:i]];

        roomratetxt.textAlignment = NSTextAlignmentRight;

        [roomratetxt setTag:i];

        [roomratetxt setDelegate:(id)self];

        [scrlsummery addSubview:roomratetxt];

    }

}

答案 2 :(得分:0)

所有5个textField的框架都放在相同的位置。您可能还需要更改其框架。

for (int i = 0; [myarray count]; i ++){

roomratetxt = [[UITextField alloc] init];

roomratetxt.frame = CGRectMake(200, i*60, 90, 20);

roomratetxt.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lighttextcolor.png"]];

roomratetxt.backgroundColor = [UIColor clearColor];

roomratetxt.font = [UIFont systemFontOfSize:12];

roomratetxt.text = [NSString stringWithFormat:@"%@",[myarray objectAtIndex:i]];

roomratetxt.textAlignment = NSTextAlignmentRight;

[roomratetxt setTag:i];

[roomratetxt setDelegate:(id)self];

[scrlsummery addSubview:roomratetxt];

}