目标C:如何使用addTarget:action:forControlEvents:方法?

时间:2011-05-19 06:28:40

标签: objective-c ios events uipicker

我试图在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];
}

2 个答案:

答案 0 :(得分:16)

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

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

此外,updateDate方法应采用id。

类型的对象
- (void) updateDate:(id) obj { }

答案 1 :(得分:2)

更改代码 -

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

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

你也可以在UIPicker代表中处理它。