user3186310 user3186310 - 1 month ago 3x
Objective-C Question

Sending data from a Text Field to a Mutable Array

I have a textfield that I am trying to have the data sent to a Mutable Array when the user presses done. The only way this works is when I do

textFieldArray = [[NSMutableArray alloc]initWithObjects:self.textField.text, nil];
but then it rewrites the mutable array when the user taps adds another object.
Here is my code:

- (IBAction)doneButton:(id)sender {

[self resignFirstResponder];

[textFieldArray addObject:self.textField.text];

NSLog(@"array: %@", textFieldArray);

This may be flagged as a duplicate question but I cannot find anything that solves my problem, Thanks for helping.


Just add this to viewDidLoad: method and it should work fine:

textFieldArray = [[NSMutableArray alloc]init];

You need to allocate and initialise this array,but not every time you adding new object in Array.