将日期选择器日期传递回View Controller以在标签中显示日期

时间:2014-07-27 04:01:31

标签: ios uitableview uiviewcontroller uilabel nsdate

我已经研究了很多关于如何做到这一点的问题,而且我的工作时间很短。

我有ViewControllerA和ViewControllerB。

ViewControllerB正在将NSDateUIDatePicker传递回ViewControllerA

我很好,直到尝试将NSDate作为标签放在与其对应的TableViewCell中。

你能帮忙吗?谢谢!

ViewControllerA

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

item是从ViewControllerB中选择的日期。如何让它在相应的TableViewCell中显示为标签?

2 个答案:

答案 0 :(得分:1)

使用delegate传递日期或其他选项是发送Notificaition

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}];
}

答案 1 :(得分:1)

在didSelectRowAtIndexPath中(或者在prepareForSegue中,如果您正在使用它)在属性中保存所选单元格的indexPath。然后,在您的委托方法中,将项添加到模型中(无论您使用哪个填充表视图),然后使用保存的indexPath调用reloadRowsAtIndexPath:来更新表。