Reputation: 3432
I am pretty new to Objective-C, and trying to create a to-do list app with a table view. I am trying to keep adding strings to my mutable array as the button is pressed. But each time I press the button only the last string gets added to the array.
- (IBAction)notebutton:(UIButton *)sender {
NSMutableArray *mystr = [[NSMutableArray alloc] init];
NSString *name = _noteField.text;
[mystr addObject:name];
[self.tableView reloadData];
}
Upvotes: 0
Views: 104
Reputation: 519
You Are creating mutable array on method. so each time its Allocating and making new array.
Just allocate the nsmutable array in viewdidload/viewdidappear and add object in your method.
Upvotes: 0
Reputation: 31311
Do not declare the NSMutableArray
each time. Declare it only once.
Make NSMutableArray *mystr;
as a property and allocate it in viewDidLoad()
once.
in .h or .m file
@property(nonatomic,strong) NSMutableArray *mystr;
viewDidLoad()
self.mystr = [[NSMutableArray alloc] init];
- (IBAction)notebutton:(UIButton *)sender {
NSString *name = _noteField.text;
[self.mystr addObject:name];
[self.tableView reloadData];
}
Upvotes: 5