开发者

popToRootViewControllerAnimated and reloadData

开发者 https://www.devze.com 2022-12-22 18:05 出处:网络
I\'ve written a tabbar appli开发者_JAVA技巧cation where on the first tab i have a tableview with a navigation controller.

I've written a tabbar appli开发者_JAVA技巧cation where on the first tab i have a tableview with a navigation controller.

The tableviewController gets pushed every time i select a row. This is a remote directory on a Server e.g. /dir1

When from the second tab i select a differnt root directory e.g /dir2 then when i go to the first tab i want to pop all the controllers off the stack and reload the table view with the contents of /dir2. So this is what i do

- (void)viewWillAppear:(BOOL)animated
{
   [[self navigationController] popToRootViewControllerAnimated:NO];    
   [self initFirstLevel];   // This loads the data.     
   [self.tableView reloadData];
}

What happens is the tableviewControllers get poped off the stack and returns to the rootViewController but the contents of /dir2 doesn't get loaded in the table view.


When you call the

[[self navigationController] popToRootViewControllerAnimated:NO];

the navigationController will try to pop all view controllers and show the topview controller, the following codes will not be called.

You should consider handling the viewWillAppear method of the topViewController for any modifications and reloading of data.

This is an example of what you can do with viewWillAppear on the sample application iPhoneCoreDataRecipes That sample application will give you an overview of view controller's lifecycles, etc...

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    [photoButton setImage:recipe.thumbnailImage forState:UIControlStateNormal];
    self.navigationItem.title = recipe.name;
    nameTextField.text = recipe.name;    
    overviewTextField.text = recipe.overview;    
    prepTimeTextField.text = recipe.prepTime;    
    [self updatePhotoButton];

    /*
     Create a mutable array that contains the recipe's ingredients ordered by displayOrder.
     The table view uses this array to display the ingredients.
     Core Data relationships are represented by sets, so have no inherent order. Order is "imposed" using the displayOrder attribute, but it would be inefficient to create and sort a new array each time the ingredients section had to be laid out or updated.
     */
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"displayOrder" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];

    NSMutableArray *sortedIngredients = [[NSMutableArray alloc] initWithArray:[recipe.ingredients allObjects]];
    [sortedIngredients sortUsingDescriptors:sortDescriptors];
    self.ingredients = sortedIngredients;

    [sortDescriptor release];
    [sortDescriptors release];
    [sortedIngredients release];

    // Update recipe type and ingredients on return.
    [self.tableView reloadData]; 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号