我搜索并查看了许多类似的问题,但仍然找不到答案。如何在不初始化它的另一个实例的情况下引用不同的类?如何从单独的类中调用“reloadData”以反映 MOC 的数据。MOC 似乎正在保存,因为我已经通过 NSNotification 验证了它。
弹出框类:
-(void)actionSave:(id)sender {
MainContent *entityContent =
[NSEntityDescription insertNewObjectForEntityForName:@"MainContent"
inManagedObjectContext:self.mDoc.managedObjectContext];
entityContent.date = [NSDate date];
entityContent.title = self.titleName.text;
//Main Question: How do I call the ViewController's function that is in a separate class?
[ViewController reloadTableView]; //???
//Separate Question: How do I dismiss this popover from inside of this function?
}
视图控制器类:
-(void)setupTable {
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(gridV1, 140, column1_width, 768-170) style:UITableViewStylePlain];
[self.tableView setBackgroundColor:[UIColor clearColor]];
self.tableView.autoresizesSubviews = YES;
self.tableView.delegate = self.tableViewController;
self.tableView.dataSource = self.tableViewController;
[self.view addSubview:self.tableView];
}
-(void)reloadTableView{
[self.tableView reloadData];
}
TableViewController 类:(self.tableView 的委托和数据源)
- (void)setupFetchedResultsController
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MainContent"];
// I want to sort them by the "date" attribute input by [NSDate date].
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];
// no predicate because we want ALL of the data in the "MainContent" entity.
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.mDoc.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
}
- (void)setMDoc:(UIManagedDocument *)mDoc {
if (_mDoc != mDoc) _mDoc = mDoc;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.mDoc) {
[[globalMDoc sharedDocumentHandler] performWithDocument:^(UIManagedDocument *coreDatabase) {
self.mDoc = coreDatabase;
[self setupFetchedResultsController];
}];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Overview Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
MainContent *content = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = content.title;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [content date]];
return cell;
}