r3n
r3n

Reputation: 27

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 : screenshot the fields got the following names and the tag-value 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. The value will be counted to 2 for the second pair of Fields, 3 for the third pair etc.

I want to foreach an array which is as long as the number of textField pairs and count 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. How do I extend this selection by the elements name?

UITextField *textField = (UITextField*)[self.view viewWithTag:1];
NSString *amount1 = textField.text;

Upvotes: 2

Views: 2516

Answers (3)

Mallikarjuna SB
Mallikarjuna SB

Reputation: 40

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

Tag of the amount textfield for nth row should be (n * 2) + 2 where 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;

Upvotes: 0

Stonz2
Stonz2

Reputation: 6396

I would suggest moving this all into a UITableView. There are many ways to modify the way it looks by using custom UITableViewCells and adjusting the table's background color, separator insets, etc.

Look around for a tutorial that you like for creating a custom cell, and give it your 2 UITextFields as @property variables. Create a new UITableViewController instead of your existing controller and #import your custom cell class.

TableViewController.h

@interface TableViewController : UITableViewController <UITextFieldDelegate>
{
    NSMutableArray *namesArray;
    NSMutableArray *amountsArray;
    UITextField *activeField;
}

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]
}

Upvotes: 1

Duyen-Hoa
Duyen-Hoa

Reputation: 15784

You can use tag with value: 1000 + i pour name and 2000 + j pour amount.

Then, to get value of name associated, use tag = tagOfName + 1000.

Upvotes: 0

Related Questions