Realinstomp
Realinstomp

Reputation: 532

Pass Date Picker date back to View Controller to show Date in label

I've researched a bunch of questions on how to do this, and am coming up just short.

I have ViewControllerA and ViewControllerB.

ViewControllerB is passing the NSDate from the UIDatePicker back to ViewControllerA.

I'm fine until trying to put that NSDate as a label in the TableViewCell it corresponds with.

Can you help? Thanks!

ViewControllerA

- (void)addItemViewController:(EXEDurationPickerViewController *)controller didFinishEnteringItem:(NSString *)item {
    NSLog(@"This was returned from ViewControllerB %@", item);
}

item is the Date picked from ViewControllerB. How do I get it to show up as a label in the corresponding TableViewCell?

Upvotes: 0

Views: 511

Answers (2)

codester
codester

Reputation: 37189

Use delegate to pass the date or other option is send Notificaition

Add this in ViewControllerA

@interface ViewControllerA : UIViewController{
    NSIndexPath *selectedIndexPath;
}
@end

-(void)viewDidLoad{

  [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveNotification:) 
        name:@"dateSend"
        object:nil];
}

- (void) receiveNotification:(NSNotification *) notification
{
   NSString *item =  notification.userInfo[@"date"];

   // show for what cell you want to show
    //keep selectedIndexPath as instance Variable
    YourCell *cell = (YourCell *)[self.tableView cellForRowAtIndexPath:selectedIndexPath];
    cell.label.text = item;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedIndexPath = indexPath;
}
//Post the notification fro `ViewControllerB`
- (void)addItemViewController:(EXEDurationPickerViewController *)controller didFinishEnteringItem:(NSString *)item {
    NSLog(@"This was returned from ViewControllerB %@", item);


    [[NSNotificationCenter defaultCenter] postNotificationName: @"TestNotification" object:nil userInfo:@{@"date":item}];
}

Upvotes: 1

rdelmar
rdelmar

Reputation: 104092

In the didSelectRowAtIndexPath (or in prepareForSegue if you're using that instead) save the indexPath of the selected cell in a property. Then, in your delegate method, add item to your model (whatever you're populating your table view with), and then call reloadRowsAtIndexPath: with that saved indexPath to update the table.

Upvotes: 1

Related Questions