首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >popToRootViewControllerAnimated和reloadData

popToRootViewControllerAnimated和reloadData
EN

Stack Overflow用户
提问于 2010-03-11 02:04:19
回答 1查看 6.9K关注 0票数 3

我已经编写了一个选项卡栏应用程序,在第一个选项卡上有一个带有导航控制器的表视图。

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

当我从第二个选项卡中选择一个不同的根目录时,例如/dir2,然后当我转到第一个选项卡时,我想从堆栈中弹出所有控制器,并使用/dir2的内容重新加载表视图。所以这就是我要做的

代码语言:javascript
复制
- (void)viewWillAppear:(BOOL)animated
{
   [[self navigationController] popToRootViewControllerAnimated:NO];    
   [self initFirstLevel];   // This loads the data.     
   [self.tableView reloadData];
}

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

EN

回答 1

Stack Overflow用户

发布于 2010-03-11 02:09:07

当您调用

代码语言:javascript
复制
[[self navigationController] popToRootViewControllerAnimated:NO];

navigationController将尝试弹出所有视图控制器并显示顶部视图控制器,以下代码将不会被调用。

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

这是一个在示例应用程序iPhoneCoreDataRecipes上使用viewWillAppear的示例,该示例应用程序将向您概述视图控制器的生命周期,等等……

代码语言:javascript
复制
- (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]; 
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2419326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档