12

我正在尝试在 UIPicker 的数据发生更改(实时更新)后立即更新表格单元格中显示的日期和时间。我实现了以下代码。尽管更改了选择器中的值,但没有调用我的“更新”方法。任何人都可以建议吗?谢谢!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSUInteger row = [indexPath row];
    if (row == 0) 
    {
        self.picker.hidden = NO;

        [self.picker addTarget:self action:@selector(updateDate) forControlEvents:UIControlEventTouchUpInside];

    }

}

- (void)updateDate
{
    selectedDate = [self.picker date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd-MM-yyyy HH:mm"];

    selectedDateString = [formatter stringFromDate:selectedDate];

    [tableView reloadData];
}
4

2 回答 2

18

您需要在选择器名称后加一个冒号。

[self.picker addTarget:self action:@selector(updateDate:) forControlEvents:UIControlEventTouchUpInside];

此外,updateDate 方法应该采用 id 类型的对象。

- (void) updateDate:(id) obj { }
于 2011-05-19T06:52:40.540 回答
1

更改您的代码 -

[self.picker addTarget:self action:@selector(updateDate:) forControlEvents:UIControlEventTouchUpInside];

[self.picker addTarget:self action:@selector(updateDate:) forControlEvents:UIControlEventAllEvents];

你也可以在 UIPicker 委托中处理它。

于 2011-05-19T07:27:26.910 回答