0

Im having 8 UITextFields,i want to store each textfields input value in NSMutableArray ?Can anyone help me to code?

5 Answers 5

4

It will be beneficial to you if you store all values in dictionary format.

Still if you want to store in NSMutableArray then you can do like this :

 - (void)textFieldDidEndEditing:(UITextField *)textField
 {
      [mutArray addObject:textField.text];
 }
Sign up to request clarification or add additional context in comments.

7 Comments

Please take a look before submit your answer [mutArray addObject:textField.text];
By the way , we can use the default textfield name that we have given in their IBOutlets.So my answer was correct.Still i've edited it.
But give your answer according to question i want to store each textfields input value in NSMutableArray
Every time the textfield ends editing this method will be called and every time the text will be saved in array.I just missed to make the letter capital.That's it.
In this way better to store in dictionary where key is textfield and value is text property
|
1

Why not you can try connecting those textfields into an outlet connection like below so that you can keep track of those textfield values where ever you want?

@property (retain, nonatomic) IBOutletCollection(UITextField) NSArray *textFieldApp;

Comments

0

In case if You need only save one time :

NSMutableArray *fieldValues = [NSMutableArray arrayWithObjects:
             textfield1.text, 
             textfield2.text,
             textfield3.text, 
             textfield4.text, 
             textfield5.text, 
             textfield6.text, 
             textfield7.text, 
             textfield8.text, nil];

In case if you need save many times, best solution is using NSMutableDictionary (use tag as key):

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [dict setObject:txtfield.text forKey:textField.tag];
}

Comments

0

Use setValue for key in NSMutableArray

NSMutableArray *testArray = [NSMutableArray array];
[testArray setValue:textFiled1InputString forKey:@"field1"];

or if you want to use index then use:

-insertObject:atIndex: and replaceObjectAtIndex:withObject:.

of NSMutableArray

Comments

0

Make sure that all text fields are tagged and use this code

- (void)store {

    NSMutableArray *arr = [NSMutableArray array];

    for (int i = 1; i <= 8; i++) {
        UITextField *tf = (UITextField *)[self.view viewWithTag:i];
        if (tf && [tf isKindOfClass:[UITextField class]] && tf.text.length > 0) {
            [arr addObject:tf.text];
        } else {
            [arr addObject:@"empty"];
        }
    }
    NSLog(@"All values : %@", arr);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.