当我们使用dismissModalViewControllerAnimated返回到上一个视图时如何重新加载数据?

发布于 2024-12-08 06:23:25 字数 1606 浏览 0 评论 0原文

当我使用 dismissModalViewControllerAnimated 时,我无法在表视图中重新加载数据,但是如果我使用 pushViewController ,它就可以完美工作。

我正在 viewWillAppear 中调用 reloadData

这就是我切换视图的方式:

- (IBAction)addAction:(id)sender
{
    NSLog(@"Add Button Pressd");
    AddNewDrinks *newView = [[AddNewDrinks alloc] initWithNibName:@"AddNewDrinks" bundle:nil];
    self.addNewDrink = newView;
    [self presentModalViewController:addNewDrink animated:YES];
    [newView release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.drinkTableView reloadData];
    [super viewWillAppear:animated];  
}

这是我用来返回到上一个视图的方法。

- (IBAction)save:(id)sender
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    path = [path stringByAppendingPathComponent:@"drinks.plist"];
    NSString *drinkName = self.name.text;
    NSString *drinkIngredients = self.ingredients.text;
    NSString *drinkDirection = self.directions.text;
    NSArray *values = [[NSArray alloc] initWithObjects:drinkDirection, drinkIngredients, drinkName, nil];
    NSArray *keys = [[NSArray alloc] initWithObjects:DIRECTIONS_KEY, INGREDIENTS_KEY, NAME_KEY, nil];
    if(drinkName.length != 0)
    {
        NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
        [self.drinkArray addObject:dict];
        [dict release];
    }
    [self.drinkArray writeToFile:path atomically:YES];    
    [self dismissModalViewControllerAnimated:YES];
}

不幸的是我的表的视图数据没有重新加载。

I am unable to reload data in my table view when I use dismissModalViewControllerAnimated however it works perfect if I use pushViewController.

I am calling reloadData in viewWillAppear.

This is how I am switching views:

- (IBAction)addAction:(id)sender
{
    NSLog(@"Add Button Pressd");
    AddNewDrinks *newView = [[AddNewDrinks alloc] initWithNibName:@"AddNewDrinks" bundle:nil];
    self.addNewDrink = newView;
    [self presentModalViewController:addNewDrink animated:YES];
    [newView release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.drinkTableView reloadData];
    [super viewWillAppear:animated];  
}

This is what i used to get back to previous view.

- (IBAction)save:(id)sender
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    path = [path stringByAppendingPathComponent:@"drinks.plist"];
    NSString *drinkName = self.name.text;
    NSString *drinkIngredients = self.ingredients.text;
    NSString *drinkDirection = self.directions.text;
    NSArray *values = [[NSArray alloc] initWithObjects:drinkDirection, drinkIngredients, drinkName, nil];
    NSArray *keys = [[NSArray alloc] initWithObjects:DIRECTIONS_KEY, INGREDIENTS_KEY, NAME_KEY, nil];
    if(drinkName.length != 0)
    {
        NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
        [self.drinkArray addObject:dict];
        [dict release];
    }
    [self.drinkArray writeToFile:path atomically:YES];    
    [self dismissModalViewControllerAnimated:YES];
}

Unfortunately my table's view data is not reloading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野却迷人 2024-12-15 06:23:25

从表面上看,您正在将编辑后的数组写入文件,但在重新加载表之前不会将其读回。

在 viewWillAppear 中,尝试将新文件读入内存,然后重新加载表。

From the looks of it, you're writing an edited array to a file, but not reading it back before reloading the table.

In viewWillAppear, try reading the new file into memory, then reloading the table.

此刻的回忆 2024-12-15 06:23:25

关闭模态视图控制器后是否调用 viewWillAppear?
您的表是有效对象吗?

Is viewWillAppear called after dismissing the modal view controller?
Is your table a valid object?

御弟哥哥 2024-12-15 06:23:25

您是否从文件中读取表格视图中显示的数据?

Are you reading from a file the data you are presenting in the tableview?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文