3

I am in the process of converting the Apple DateCell app to use UIPickerView instead of UIDatePicker. I've pulled out and replaced references to UIDatePicker with UIPickerView in storyboard and throughout the code. Still have some clean up to do on the references to DatePicker but, I am at a point where i need to convert the reference to the "dateAction" method from the "ValueChanged" event of the UIDatePicker and there is no "Send Events" what so ever for UIPickerView. Does anyone have any suggestions on how I can add or simulate this "ValueChanged" event to my UIPickerView? Also, any advice on converting the rest of the program to use UIPickerView would be greatly appreciated.

- (IBAction)dateAction:(id)sender
{
NSIndexPath *targetedCellIndexPath = nil;

if ([self hasInlineDatePicker])
{
    // inline date picker: update the cell's date "above" the date picker cell
    //
    targetedCellIndexPath = [NSIndexPath indexPathForRow:self.datePickerIndexPath.row - 1 inSection:0];
}
else
{
    // external date picker: update the current "selected" cell's date
    targetedCellIndexPath = [self.tableView indexPathForSelectedRow];
}

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:targetedCellIndexPath];
UIPickerView *targetedDatePicker = sender;

// update our data model
NSMutableDictionary *itemData = self.dataArray[targetedCellIndexPath.row];
[itemData setValue:targetedDatePicker.date forKey:kDateKey];

// update the cell's date string
cell.detailTextLabel.text = [self.dateFormatter stringFromDate:targetedDatePicker.date];

}

1 Answers1

2

UIPicker works with a delegate, not a ValueChanged handler. Make the controller the delegate of your picker and implement

– pickerView:didSelectRow:inComponent:

Don't forget to add <UIPickerViewDelegate> in the controller class declaration.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Your suggestion worked. Just a small note: make sure that you drag UIPickerView down to the bar below the view and use that object to connect your delegate to the view. If you use the UIPickerView object that is inside your table cell, Xcode throws an error. Now I just have to find the equivalent code to; "[itemData setValue:targetedDatePicker.date forKey:kDateKey];". Thanks again for your suggestion. – drvannostran98 Nov 27 '13 at 02:41
  • 1
    Not necessary. You can have a UITableViewCell containing just the picker. When dequeuing the cell, identify the picker (e.g. via a `tag`) and set its delegate. The delegate can also be set in Interface Builder. – Mundi Nov 27 '13 at 09:02