谁能帮帮我?我想在循环中创建动态 text field
。我已经能够创建并使其也可编辑。我的要求在下面。
i.我想访问每个 text fields
值并将它们相加并存储在 Label
中。
二.假设有 5 个动态创建的文本字段具有值 (10,20,30,40,50)。
所以标签显示 5 值的总和。 IE。 10+20+30+40+50 = 150
如果我更改了任一 textfield
值,它应该重新计算并更改 label
中的总和。
我的逻辑如下。
-(无效)创建 {
myarr = [NSMutableArray arrayWithObjects:@"1000",@"2000",@"3000",@"4000",@"5000", nil];
int x = 20, y = 50;
int width = 280, height = 40;
for (int item=0; item<[myarr count]; item++)
{
edit_txt = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
edit_txt.text = [NSString stringWithFormat:@"%@",[myarr objectAtIndex:item]];
[edit_txt setTag:item+1];
edit_txt.placeholder = @"Click here to type";
edit_txt.borderStyle = UITextBorderStyleRoundedRect;
edit_txt.font = [UIFont systemFontOfSize:15];
edit_txt.textAlignment = NSTextAlignmentLeft;
edit_txt.returnKeyType = UIReturnKeyDefault;
edit_txt.delegate = (id)self;
[self.view addSubview:edit_txt];
y += height;
}
tot_txt = [[UITextField alloc]initWithFrame:CGRectMake(edit_txt.frame.origin.x, edit_txt.frame.origin.y + height + 10, width , height)];
tot_txt.placeholder = @"Total Value";
tot_txt.borderStyle = UITextBorderStyleRoundedRect;
tot_txt.font = [UIFont systemFontOfSize:15];
tot_txt.textAlignment = NSTextAlignmentLeft;
tot_txt.returnKeyType = UIReturnKeyDefault;
tot_txt.delegate = (id)self;
[self.view addSubview:tot_txt];
save_btn = [UIButton buttonWithType:UIButtonTypeCustom];
[save_btn setFrame:CGRectMake(tot_txt.frame.origin.x, tot_txt.frame.origin.y + height + 10, width , height)];
[save_btn setTitle:@"Save" forState:UIControlStateNormal];
[save_btn addTarget:self action:@selector(save_click:) forControlEvents:UIControlEventTouchUpInside];
[save_btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:save_btn];
CALayer *btnlayer = [save_btn layer];
[btnlayer setBorderColor:[UIColor redColor].CGColor];
[btnlayer setBorderWidth:0.3];
[btnlayer setCornerRadius:20.0];
-(void)textFieldDidEndEditing:(UITextField *)textField
{ 整数计数 = 0;
for (int item = 0; item < [myarr count]; item++)
{
UITextField *textField = ([[self.view viewWithTag:item + 1] isKindOfClass:[UITextField class]])?(UITextField *)[self.view viewWithTag:item + 1]:nil;
count += [textField.text intValue];
}
tot_txt.text = [NSString stringWithFormat:@"%d", count];
提前致谢。 阿 git ·库马尔。
最佳答案
会更简单,
要访问每个文本字段(范围仅在循环内可用),您可以使用 tag
文本字段的属性。
示例:
for (int itemIndex = 1; itemIndex <= 5; itemIndex++)
{
UITextField *textField = [[UITextField alloc] initWithFrame:yourFrame];
//all properties assigning here
textField.tag = itemIndex; //assigning tag here(starts from 1).
textField.delegate = self;
[self.view addSubview: textField];
}
现在,计算来了。实现 UITextField
代表们。
写<UITextFieldDelegate>
在你的 .h 文件中
@interface yourClassName : UIViewController <UITextFieldDelegate>
在结束编辑时进行计算,
-(void)textFieldDidEndEditing:(UITextField *)textField
{
int count = 0;
for (int itemIndex = 1; itemIndex <= 5; itemIndex++)
{
UITextField *textField = ([[self.view viewWithTag:itemIndex] isKindOfClass:[UITextField class]])?(UITextField *)[self.view viewWithTag:itemIndex]:nil;
count += [textField.text intValue];
}
countLabel.text = [NSString stringWithFormat:@"%d", count];
}
要在编辑时进行计算,请执行委托(delegate) shouldChangeCharactersInRange
并使用相同的代码库。
同时实现委托(delegate) textFieldShouldClear
,
textFieldShouldReturn
并使用相同的代码库。
请确保除了这些测试字段外,您的 View 不包含任何带有 1 到 5 标记的元素。
此外,您可以通过应用您的想法来避免循环 ;)
关于ios - 如何为 iOS 中的每个文本字段创建可访问和可编辑的动态文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28123942/