0

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?

Realinstomp
  • 532
  • 2
  • 13
  • 30

2 Answers2

1

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}];
}
codester
  • 36,891
  • 10
  • 74
  • 72
  • Hi! Thanks for the help! The only part I'm not totally tracking on is `cellForRowAtIndexPath:selectedIndexPath];` what I would use as `selectedIndexPath`. Can you help me understand that a bit better? – Realinstomp Jul 27 '14 at 04:35
  • on `didSelectRowAtIndexPath` save the indexPath to some intstance variable say `NSIndexPath *selectedIndexPath` and on `didSelectRowAtIndexPath` assign it to `selectedIndesPath` – codester Jul 27 '14 at 07:41
1

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.

rdelmar
  • 103,982
  • 12
  • 207
  • 218