[英]How to get the value of an textField by its name and tag - objective c
i got a view where the user is can input a name and an age : 我有一个视图,用户可以在其中输入姓名和年龄: the fields got the following names and the tag-value 1. 字段具有以下名称和标记值1。
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *amount;
If the user creates more textFields, they will be also named *name und *age and linked to the ViewController. 如果用户创建更多textField,它们也将被命名为* name und * age并链接到ViewController。 The value will be counted to 2 for the second pair of Fields, 3 for the third pair etc. 对于第二对字段,该值将被计为2,对于第三对字段将被计为3,以此类推。
I want to foreach an array which is as long as the number of textField pairs and count i++; 我想foreach一个数组,该数组的长度与textField对的数量一样,并且要计数i ++; How do is the syntax for getting the values referring to name and tag? 获取引用名称和标签的值的语法如何? I googled and found the following solution which works if the tags are unique, which is not the case in my situation. 我在Google上搜索并找到了以下解决方案,这些解决方案在标签是唯一的情况下有效,在我的情况下情况并非如此。 How do I extend this selection by the elements name? 如何通过元素名称扩展此选择?
UITextField *textField = (UITextField*)[self.view viewWithTag:1];
NSString *amount1 = textField.text;
I would suggest moving this all into a UITableView
. 我建议将所有这些都移到UITableView
。 There are many ways to modify the way it looks by using custom UITableViewCell
s and adjusting the table's background color, separator insets, etc. 通过使用自定义UITableViewCell
并调整表格的背景色,分隔符插入等多种方法可以修改其外观。
Look around for a tutorial that you like for creating a custom cell, and give it your 2 UITextField
s as @property
variables. 环顾四周,查找您喜欢的用于创建自定义单元的教程,并将其2 UITextField
作为@property
变量。 Create a new UITableViewController
instead of your existing controller and #import
your custom cell class. 创建一个新的UITableViewController
而不是您现有的控制器,并#import
您的自定义单元格类。
TableViewController.h TableViewController.h
@interface TableViewController : UITableViewController <UITextFieldDelegate>
{
NSMutableArray *namesArray;
NSMutableArray *amountsArray;
UITextField *activeField;
}
TableViewController.m TableViewController.m
// only relevant code displayed
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [namesArray count] + 1; // extra row for the "Add Row" row
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 100.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIButton *addDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addDataButton setTitle:@"Add Data to Array"];
[addDataButton addTarget:self action:@selector(addDataToArray) forControlEvents:UIControlEventTouchUpInside];
// customize your button as needed
UITableViewHeaderFooterView *footerView = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
[footerView addSubview:addDataButton];
return footerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == [namesArray count]) // the last row
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
cell.textLabel.text = @"Add Row";
return cell;
}
static NSString *reuseIdentifier = @"YourReuseID"; // use whatever you set in your custom cell class
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil] objectAtIndex:0];
}
cell.nameField.text = [namesArray objectAtIndex:indexPath.row];
cell.nameField.tag = indexPath.row;
cell.nameField.delegate = self;
cell.amountField.text = [amountsArray objectAtIndex:indexPath.row];
cell.amountField.tag = indexPath.row + 1000000; // to make sure it doesn't have the same tag as your nameField
cell.amountField.delegate = self;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row = [namesArray count])
{
[namesArray addObject:@""];
[amountsArray addObject:@""];
[tableView reloadData];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeTextField = textField;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if(textField.tag < 1000000)
{
[namesArray replaceObjectAtIndex:textField.tag withObject:textField.text];
}
else
{
[amountsArray replaceObjectAtIndex:textField.tag - 1000000 withObject:textField.text];
}
}
- (void)addDataToArray
{
[activeTextField resignFirstResponder];
// add your data as needed. The first row's name/amount will be available in [namesArray objectAtIndex:0] and [amountsArray objectAtIndex:0]
}
You can use tag with value: 1000 + i pour name and 2000 + j pour amount. 您可以使用具有以下值的标记:1000 +我倒入名称和2000 + j倒入量。
Then, to get value of name associated, use tag = tagOfName + 1000. 然后,要获取关联的名称值,请使用tag = tagOfName + 1000。
you need to have a logic which uniquely identifies textfields. 您需要具有一种可以唯一标识文本字段的逻辑。
Set the tags of name and amount as mentioned below. 如下所述设置名称和金额标签。
Tag of the name textfield for nth row should be (n * 2) + 1 where n=0,1,2.....n-1 第n行的名称文本字段的标记应为(n * 2)+1,其中n = 0,1,2 ..... n-1
Tag of the amount textfield for nth row should be (n * 2) + 2 where n=0,1,2.....n-1 第n行的金额文本字段的标记应为(n * 2)+ 2,其中n = 0,1,2 ..... n-1
You can get the value associated with textfield by using the below statements 您可以使用以下语句获取与文本字段关联的值
UITextField *nameNTextField = (UITextField*)[self.view viewWithTag:(n * 2) + 1]; //where n=0,1,2.....n-1
NSString *nameN = nameTextField.text;
UITextField *amountNTextField = (UITextField*)[self.view viewWithTag:(n * 2) + 2]; //where n=0,1,2.....n-1
NSString *amountN = nameTextField.text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.