我已经编写了一个tabbar应用程序,在第一个选项卡上我有一个带导航控制器的tableview。
每次选择一行时,tableviewController都会被推送。这是服务器上的远程目录,例如/ DIR1
当从第二个选项卡中选择一个不同的根目录,例如/ dir2然后当我转到第一个选项卡时,我想要将所有控制器从堆栈中弹出并使用/ dir2的内容重新加载表视图。 所以这就是我做的事情
- (void)viewWillAppear:(BOOL)animated
{
[[self navigationController] popToRootViewControllerAnimated:NO];
[self initFirstLevel]; // This loads the data.
[self.tableView reloadData];
}
tableviewControllers从堆栈中跳出并返回到rootViewController但是/ dir2的内容没有加载到表视图中。
答案 0 :(得分:4)
当你致电
时[[self navigationController] popToRootViewControllerAnimated:NO];
navigationController将尝试弹出所有视图控制器并显示topview控制器,以下代码将不会被调用。
您应该考虑使用topViewController的viewWillAppear方法来修改和重新加载数据。
这是您可以对示例应用程序iPhoneCoreDataRecipes上的viewWillAppear执行的操作示例。该示例应用程序将为您提供视图控制器的生命周期等概述...
- (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];
}