Reputation: 13
I searched and looked over many similar questions, yet still cannot find the answer. How do I reference a different class without initializing another instance of it? How do I call "reloadData" from a separate class to reflect the MOC's data. The MOC seems to be saving as I have verified it by NSNotification.
Popover Class:
-(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?
}
ViewController Class:
-(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 Class: (self.tableView's delegate and datasource)
- (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;
}
Upvotes: 1
Views: 1305
Reputation: 12444
Okay so make a singleton like the example from this link: http://www.galloway.me.uk/tutorials/singleton-classes/
Then make an ivar in the Singleton for your UIViewController that your UITableView is in. Then in the VDL of the UITableView view, do something like
[Singleton sharedSingleton].theViewController = self;
Then in the other view do something like
Singleton *singleton = [Singleton sharedSingleton];
[singleton.mytableview reloadData];
However, for the (= self) line, you may want to do it somewhere earlier like the applicationDidFinishLaunching so that the theViewController variable is not nil when you try to access it.
Upvotes: 1
Reputation: 5973
Personally I use NSNotifcations, I have a couple of events that occur asynchronously, and I post a notification back.
That is, what I do is download a picture on a background thread, and reload the table once it finishes. I do this by sending a notification to the tableviewclass, and the table view class reloads its own tableview.
~ Dan
Upvotes: 1