Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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];
}
like image 557
Zhen Avatar asked May 19 '11 06:05

Zhen


1 Answers

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 { }
like image 180
csano Avatar answered Oct 16 '22 18:10

csano