I need to add contents of textfield named productName to an NSMutableArray named cartArray by clicking on a button.
In the button action event I wrote:
NSString *productName = [NSString stringWithFormat:@"%@", productName.text];
cartArray = [[NSMutableArray alloc] init];
[cartArray addObject:productName];
NSLog(@"Cart Array==%@", cartArray);
But i need to add that content of textfield each time user click on button. Here in console previous content in array not showing i.e. it’s not get added each time.
Suggest me anything I need to do in viewDidLoad or anywhere else?
You are re-creating the cartArray each time the button is pressed, thus overwriting the previous entry. Move the initialisation of the array to viewDidLoad or your init method.
Also productName.text is already a string, there is no need to do the stringWithFormat.