gpt4 book ai didi

iphone - popToRootViewControllerAnimated 和 reloadData

转载 作者:行者123 更新时间:2023-12-03 19:42:52 26 4
gpt4 key购买 nike

我编写了一个选项卡栏应用程序,其中第一个选项卡上有一个带有导航 Controller 的表格 View 。

每次我选择一行时,tableviewController 都会被推送。这是服务器上的远程目录,例如/目录1

当从第二个选项卡中选择不同的根目录(例如/dir2)时,当我转到第一个选项卡时,我想将所有 Controller 从堆栈中弹出,并使用/dir2 的内容重新加载 TableView 。这就是我所做的

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

发生的情况是 tableviewControllers 从堆栈中弹出并返回到 rootViewController,但/dir2 的内容不会加载到 TableView 中。

最佳答案

当您调用

[[self navigationController] popToRootViewControllerAnimated:NO];

navigationController会尝试弹出所有 View Controller 并显示topview Controller ,以下代码将不会被调用。

您应该考虑处理 topViewController 的 viewWillAppear 方法以进行任何修改和重新加载数据。

这是一个示例,说明您可以在示例应用程序 iPhoneCoreDataRecipes 上使用 viewWillAppear 执行哪些操作该示例应用程序将为您提供 View Controller 生命周期等的概述...

- (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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com