我编写了一个标签栏应用程序,其中在第一个标签上,我具有带有导航控制器的表格视图。
每当我选择一行时,tableviewController都会被推送。这是服务器上的远程目录,例如/目录1
当从第二个选项卡中选择一个不同的根目录,例如/ dir2时,然后当我转到第一个选项卡时,我想将所有控制器弹出堆栈,并用/ dir2的内容重新加载表视图。
所以这就是我要做的
- (void)viewWillAppear:(BOOL)animated
{
[[self navigationController] popToRootViewControllerAnimated:NO];
[self initFirstLevel]; // This loads the data.
[self.tableView reloadData];
}
发生的情况是tableviewControllers从堆栈弹出并返回到rootViewController,但是/ dir2的内容未在表视图中加载。
最佳答案
当您致电
[[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];
}
关于iphone - popToRootViewControllerAnimated和reloadData,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2419326/