我知道这个问题有点老了,但通过回答我可以帮助别人。
我想我找到了解决这个问题的方法。
解决方案
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
// Passing the row being viewed to a function that does something with it.
[self handleRowBeingViewed:[pickerView selectedRowInComponent:component]];
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
// Passing the row being viewed to a function that does something with it.
[self handleRowBeingViewed:[pickerView selectedRowInComponent:component]];
}
/**
* It receives, in input 'rowBeingViewed', the row being viewed by the user in the center of the picker view, and use it to XYZW...
*
*
* @param rowBeingViewed Row currently being viewed by the user in the center of the picker view.
* @return
*/
- (void) handleRowBeingViewed:(NSInteger)rowBeingViewed
{
// Printing for debugging.
NSLog(@"String Being Viewed: %@", pickerViewArray[rowBeingViewed]);
// DO YOUR STUFF HERE
// ...
}
解释
UIPickerView让您可以随时使用 selectedRowInComponent 方法知道选择了哪一行(或在中心查看,我认为是相同的)。
如果您在委托的方法pickerView:titleForRow:forComponent中使用该方法selectedRowInComponent,则每次在选取器视图中向用户“打印”新标题时,您都可以知道在中心查看的是哪一行。
有时,如果在 Picker View 中只滚动 1 或 2 行,则不会调用委托的方法pickerView:titleForRow:forComponent并且您将不知道在中心查看的是哪一行。为了解决这个问题,还要在委托的方法pickerView:didSelectRow:inComponent:中使用相同的方法selectedRowInComponent。
希望这可以帮助某人。