Reputation: 277
Im having 8 UITextFields
,i want to store each textfields input value in NSMutableArray
?Can anyone help me to code?
Upvotes: 0
Views: 1752
Reputation: 2357
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];
}
Upvotes: 4
Reputation: 5038
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);
}
Upvotes: 0
Reputation: 3738
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;
Upvotes: 1
Reputation: 4624
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
Upvotes: 0
Reputation: 5657
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];
}
Upvotes: 0