Im having 8 UITextFields,i want to store each textfields input value in NSMutableArray ?Can anyone help me to code?
5 Answers
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];
}
7 Comments
Rajneesh071
Please take a look before submit your answer [mutArray addObject:textField.text];
Mansi Panchal
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.
Rajneesh071
But give your answer according to question i want to store each textfields input value in NSMutableArray
Mansi Panchal
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.
NeverBe
In this way better to store in dictionary where key is textfield and value is text property
|
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
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);
}