[英]Reloading TableView Data after pop From DetailViewController
How can I reload my tableView data after I click back button/pop from my detailViewController
(using UINavigationController
and push)? 在我的
detailViewController
单击后退按钮/弹出窗口(使用UINavigationController
和push)后,如何重新加载tableView数据? I tried reloading my tableView in viewDidAppear
but nothing has changed. 我尝试在
viewDidAppear
重新加载tableView,但是没有任何改变。 Below I attached my storyboard so that you guys can have a idea about what my problem is. 在下面附上我的故事板,以便你们对我的问题有一个了解。 When I click save, I want all the data to be automatically reloaded to my tableView when I go back to my
UITableViewController
. 当我单击保存时,当我返回
UITableViewController
时,我希望所有数据都自动重新加载到tableView中。 How can I achieve this? 我该如何实现?
http://i.stack.imgur.com/i1YyV.png http://i.stack.imgur.com/i1YyV.png
Below is my code for saving new data to coreData using saveButton. 以下是我使用saveButton将新数据保存到coreData的代码。
- (IBAction)saveButton:(id)sender {
CoreDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];
NSString *name = self.nameTextfield.text;
NSString *phoneNumber = self.phoneTextfield.text;
CoreDataTableViewController *cdtvc = [[CoreDataTableViewController alloc] init];
[newContact setValue:name forKey:@"name"];
[cdtvc.name addObject:name];
[newContact setValue:phoneNumber forKey:@"phone"];
[cdtvc.phone addObject:phoneNumber];
NSError *error;
[context save:&error];
[cdtvc.tableView reloadData];
}
You are reloading the tableView
of another instance of your CoreDataTableViewController
. 您正在重新加载
CoreDataTableViewController
的另一个实例的tableView
。 With this line: 用这行:
CoreDataTableViewController *cdtvc = [[CoreDataTableViewController alloc] init];
you create a new CoreDataTableViewController
instance but when you pop the current view controller from the navigation stack another instance will be visible whose table view has not received the message -reloadData
. 您创建了一个新的
CoreDataTableViewController
实例,但是当您从导航堆栈中弹出当前视图控制器时,将看到另一个实例,该实例的表视图尚未收到消息-reloadData
。
To fix this replace the above line with this code: 要解决此问题,请使用以下代码替换上面的行:
NSArray *navigationStack = [self.navigationController viewControllers];
NSInteger previousViewControllerIndex = navigationStack.count - 2;
CoreDataTableViewController *cdtvc = [navigationStack objectAtIndex:previousViewControllerIndex];
and see if it does the trick. 看看是否能解决问题。 (Now cdtv is a reference to the original view controller on the navigation stack.)
(现在,cdtv是对导航堆栈上原始视图控制器的引用。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.