I have a main view controller that consists of 1 button that when tapped, pushes a detail view controller and another button that simply launches a modal view controller that has a table. This detail view controller has a button that when tapped should add a new object to the array that was loaded by the table in the modal view controller.
Problem is, that add a new object button doesn't add to the NSMutableArray.
When I put a button in the main view and assigned the same method of adding objects to the NSMutableArray, it works. It just doesn't work when the button is placed on another view controller presented either via Navigation Controller and Modal View.
Here's my code when the button is pressed in the detail view controller:
- (IBAction)confirmBtnPressed
{
// Gets the current time and formats it
NSDate *timeNow = [NSDate date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"hh:mm a"];
// Gets the current date and formats it
NSDate *dateNow = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy"];
NSString *currentTime = [timeFormatter stringFromDate:timeNow];
NSString *currentDate = [dateFormatter stringFromDate:dateNow];
NSString *timestamp = currentTime;
NSString *date = currentDate;
// This is where values gets saved
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:timestamp forKey:kTimeStampText];
[defaults setObject:date forKey:kDateText];
[defaults synchronize];
NSString *timeToday = [defaults objectForKey:kTimeStampText];
NSString *dateToday = [defaults objectForKey:kDateText];
tableVC.tableDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:timeToday, @"Time", dateToday, @"Date", nil];
[tableVC.tableArray addObject:tableVC.tableDict];
[tableVC.table reloadData];
}
Here's my code in the main view controller when the button to load the table modally is pressed:
// Table loaded
- (IBAction)viewHistoryPressed:(id)sender
{
if (!self.historyViewController)
{
self.historyViewController = [[HistoryViewController alloc] initWithNibName:@"HistoryViewController" bundle:nil];
}
[self presentModalViewController:historyViewController animated:YES];
}
Here's my viewDidLoad in my table view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *time = [defaults objectForKey:kTimeStampText];
NSString *date = [defaults objectForKey:kDateText];
tableDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:time, @"Time", date, @"Date", nil];
tableArray = [[NSMutableArray alloc] initWithObjects:tableDict, nil];
}
Screenshot of the main view:

detail view:

table view:

tableDict. I see the keys they are assigned to.