当我们使用dismissModalViewControllerAnimated返回到上一个视图时如何重新加载数据?
当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从表面上看,您正在将编辑后的数组写入文件,但在重新加载表之前不会将其读回。
在 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.
关闭模态视图控制器后是否调用 viewWillAppear?
您的表是有效对象吗?
Is viewWillAppear called after dismissing the modal view controller?
Is your table a valid object?
您是否从文件中读取表格视图中显示的数据?
Are you reading from a file the data you are presenting in the tableview?