0

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:

enter image description here

detail view:

enter image description here

table view:

enter image description here

5
  • Are kDateText and kTimeStampText defined anywhere? Also, make sure that time and date are not empty. Commented Jun 26, 2012 at 5:49
  • 2
    @CReaTuS I have accepted them. I just don't accept answers that don't work though. Commented Jun 26, 2012 at 5:51
  • @WrightsCS yup, it's defined as @"TimeStamp" and @"Date". Commented Jun 26, 2012 at 5:52
  • It's good practice to not accept answers that don't work. As for time / date, I mean the values for time and date in tableDict. I see the keys they are assigned to. Commented Jun 26, 2012 at 5:54
  • It just gets the current time and date the moment the button is pressed. Commented Jun 26, 2012 at 5:59

2 Answers 2

2

Try initializing your NSMutableArray with:

NSMutableArray *array = [NSMutableArray array]; 

Now you are open to adding entries.

Let me know if this works for you.

Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't do anything. I reverted it back. Thanks.
This will initialize your array... after this you should be able to add entries to this.
1

in viewDidLoad method... please use self.tableDict and self.tableArray while allocating them. 'self' calls the getter and setter methods so it is imp.

6 Comments

I'll do that now. Thanks Nishant! Do you have any other idea on how to fix this?
I hope this helps. Let me know if there are any issues with this..I'll think of some other problem that might be..
I added it but nothing happened. I just want the table to reflect the changes made to the NSMutableArray after pressing the button in the main view.
I added some screens to my question to help you visualize it.
also, may i suggest that all this approach would be much simpler if u use a singleton class for arrays and dictionaries as it'll ease out the coding process and the data (arrays etc.) can be accessed from any class and need not be passed or retrieved from another class whenever u need it. Just in case, if u r unfamiliar with Singleton classes and their usage, check out the following link. your life would be much simpler after u learn to use these :) iphonedevsdk.com/forum/iphone-sdk-tutorials/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.