2010-03-10 57 views
3

我已经写了一个tabbar应用程序,在第一个选项卡上我有一个导航控制器的tableview。popToRootViewControllerAnimated和reloadData

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

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

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

会发生什么事是tableviewControllers得到将从堆栈弹出并返回到RootViewController的但/ DIR2没有得到在表视图加载的内容。

回答

4

当你调用

[[self navigationController] popToRootViewControllerAnimated:NO]; 

的navigationController将尝试弹出所有视图控制器和显示控制器俯视图,下面的代码就不会被调用。

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

这是你可以用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'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]; 
} 
+0

你能abount处理更具体viewWillAppear方法? 我已经运行popToRootViewControllerAnimated后的代码,它似乎工作。但我不知道它在哪个控制器上加载。 – Teo 2010-03-10 18:16:19

+0

我编辑了答案 – 2010-03-10 18:28:55