samedi 7 juin 2014

Pass property from Modal View Controller to Parent


Vote count:

0




I have a tableview as a parent view controller with a child modal view controller. In the modal view controller, when users tap on a row, I'd like to set the parent's property 'filter.' However, it's just returning null.


How do I pass the NSString filter property back to its parent view? And should I be instantiating a parent view controller in the didSelectRowAtIndexPath method?


Below is the code for the modal view controller:



#import "FilterViewController.h"
#import "ContactsTableViewController.h"


@interface FilterViewController ()

@end

@implementation FilterViewController


- (void)viewDidLoad
{
[super viewDidLoad];
self.filterTable.dataSource = self;
self.filterTable.delegate = self;
[self performSelector:@selector(retrieveFilteredEvents)];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.filterEvents count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"filterTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


NSDictionary *tempDict = [self.filterEvents objectAtIndex:indexPath.row];

self.eventTitle = [tempDict objectForKey:@"eventType"];
cell.textLabel.text = self.eventTitle;

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *tempDict = [self.filterEvents objectAtIndex:indexPath.row];
NSString *string = [tempDict objectForKey:@"eventType"];

ContactsTableViewController *contactVC = [[ContactsTableViewController alloc] init];
contactVC.filter = string;

[self dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateParent" object:nil];
}

#pragma mark - Helper Methods

- (IBAction)done:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}

-(void)retrieveFilteredEvents
{
PFQuery *retrieveEvents = [PFQuery queryWithClassName:@"eventTypes"];
[retrieveEvents orderByAscending:@"eventOrder"];
[retrieveEvents findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.filterEvents = [[NSArray alloc] initWithArray:objects];
}
[self.filterTable reloadData];
}];

}

@end


asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire