PopToRootViewControllerAnimated and reloadData

I wrote a tab application where on the first tab I have a tabular view with a navigation controller.

A tableviewController is clicked every time I select a row. This is a remote directory on the server, for example. / Dir 1

When I select the root directory of differnt eg / dir2 from the second tab, then when I go to the first tab, I want to pull all the controllers from the stack and reload the table view with the contents of / dir 2. 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 when tableviewControllers exit the stack and return to rootViewController, but the contents of / dir 2 are not loaded into the table view.

+3
source share
1 answer

When you call

[[self navigationController] popToRootViewControllerAnimated:NO];

navigationController , .

viewWillAppear topViewController .

, viewWillAppear iPhoneCoreDataRecipes. ..

- (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 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]; 
}
+4

Source: https://habr.com/ru/post/1736362/


All Articles