views:

1514

answers:

3

I have a problem in my application. Any help will be greatly appreciated. Basically it is from view A to view B, and then come back from view B.

In the view A, it has dynamic data loaded in from the database, and display on the table view. In this page, it also has the edit button, not on the navigation bar. When user tabs the edit button, it goes to the view B, which shows the pick view. And user can make any changes in here. Once that is done, user tabs the back button on the navigation bar, it saves the changes into the NSUserDefaults, goes back to the view A by pop the view B.

When coming back to the view A, it should get the new data from the UIUserDefaults, and it did. I user NSLog to print out to the console and it shows the correct data. Also it should invoke the viewWillAppear: method to get the new data for the table view, but it didn't. It even did not call the tableView:numberOfRowsInSection: method. I place a NSLog statement inside this method but didn't print out in the console.

as the result, the view A still has the old data. the only way to get the new data in the view A is to stop and start the application.

both view A and view B are the subclass of UIViewController, with UITableViewDelegate and UITableViewDataSource.

here is my code in the view A :

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"enter in Schedule2ViewController ...");

    // load in data from database, and store into NSArray object

    //[self.theTableView reloadData];
    [self.theTableView setNeedsDisplay];
    //[self.theTableView setNeedsLayout];
}

in here, the "theTableView" is a UITableView variable. And I try all three cases of "reloadData", "setNeedsDisplay", and "setNeedsLayout", but didn't seem to work.

in the view B, here is the method corresponding to the back button on the navigation bar.

- (void)viewDidLoad {
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savePreference)];
    self.navigationItem.leftBarButtonItem = saveButton;
    [saveButton release];
}

- (IBAction) savePreference {
    NSLog(@"save preference.");

    // save data into the NSUSerDefaults

    [self.navigationController popViewControllerAnimated:YES];
}

Am I doing in the right way? Or is there anything that I missed?

Many thanks.

+2  A: 

When a view is first loaded, it calls the viewDidLoad method. If you create a stack, drill down into it (from A to B) and then return (B to A) the viewDidLoad does not get called again on A. What you may want to try is passing A into B (by passing in self) and call the viewDidLoad method to get the new data and then reloadData method on the the tableView to refill the table view.

What you may want to try is taking the data fetching and setting functionality out of the viewDidLoad method and place it in its own getData method. At the end of the getData method, you could place a [self.tableView reloadData]; to reset/refill the table view. From class B, you could call [self getData] and minimize the amount of work you would do in class B. This would help increase reuse-ability of that code and may prevent side effects from calling the viewDidLoad method.

GlobalSequence
A: 

You could also use viewDidAppear. It is called every time the screen appears. For performance reasons, set a flag so you don't repeat the same functionality in viewDidLoad with viewDidAppear for the first screen view.

Neffster
A: 

I have the same problem.

I try to do:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];        

}

but it doesn't work, i also tried to use - (void)viewDidAppear:(BOOL)animated but it is the same... why? I see old list in A...

Crowley