Objective C: How to use addTarget:action:forControlEvents: method?

Zhen picture Zhen · May 19, 2011 · Viewed 15.8k times · Source

I am trying to update the date and time displayed in a table cell immediately after the UIPicker's data has changed (real time updated). I implemented the following code. My "update" method is not being called despite changing the values in the picker. Can anyone advise? Thanks!

Zhen

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

Answer

csano picture csano · May 19, 2011

You need to put a colon after the selector name.

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

Also, the updateDate method should take an object of type id.

- (void) updateDate:(id) obj { }