我有一个包含选择器和 UIToolbar 的 UIActionSheet。UIToolBar 上有一个保存按钮。但是,我的一些用户报告说在 UIPickerView 停止旋转之前按下了保存按钮,因此只能检索初始值(在旋转之前)。
一旦用户在旋转时点击保存或获取活动选定项目的反馈,有没有办法获取 UIPickerView 的当前选定项目?
谢谢
我有一个包含选择器和 UIToolbar 的 UIActionSheet。UIToolBar 上有一个保存按钮。但是,我的一些用户报告说在 UIPickerView 停止旋转之前按下了保存按钮,因此只能检索初始值(在旋转之前)。
一旦用户在旋转时点击保存或获取活动选定项目的反馈,有没有办法获取 UIPickerView 的当前选定项目?
谢谢
Even if they dismiss the picker while it's still spinning, the picker will still call the delegate with the final selected row once it stops, even if it isn't visible. Assuming you haven't deallocated it yet, you can set the delegate receiver to check if the picker is visible, and if it isn't, save the selected value.
I do this assuming it's clear the user isn't scrolling to a random value - usually when they scroll and dismiss without waiting for it to settle, it means they scrolled to either the very top or very bottom of the list. I'd say you can safely use the result of the delegate in these two cases.
你有没有想过用这个titleForRow
方法来解决这个问题。随着pickerview
滚动,您的titleForRow
方法被调用以填充当前查看的行上方或下方的行(取决于选择器旋转的方式)。When the picker stops, the didSelectRow
method is called and the row returned is 2 rows from the last row populated in the titleForRow
method.
我认为您可以选择titleForRow
在滚动中的某个时间点停止选择器(用于检测用于告诉代码从调用中捕获组件和行的点的计时器),然后selectRow
将值设置为您想要的值它是在它停止旋转(并被didSelectRow
调用)之后。
我不认为这是一个你可以自己解决的问题UIPickerView
。
现在没有动画停止就无法选择哪一行(因此选择器视图选择它停止的行)。唯一的方法是告诉选择器在哪一行停止,selectRow:inComponent:animated:
但你怎么知道那是哪一行?你不知道,因为选择器在旋转...
我认为这只是一个限制,UIPickerView
Apple 可能会将其描述为预期的行为。
刚刚找到解决方案,适用于我的情况。我不需要最后一个更新事件,该事件是在用户关闭后选择器停止旋转时生成的。所以这个方法停止滚动并且不会导致新的更新选择事件。似乎您将在此之后检查选择器中的选择是否正常(我不在乎,当我显示选择器时我会重新选择值)。
- (void)_resetPickerViewDelegates {
id delegate = [_pickerView.delegate retain];
id dataSource = [_pickerView.dataSource retain];
_pickerView.delegate = nil;
_pickerView.dataSource = nil;
[_pickerView reloadAllComponents];
_pickerView.delegate = delegate;
_pickerView.dataSource = dataSource;
[delegate release];
[dataSource release];
}
我遇到了同样的问题...我通过在关闭或选择选择器值的方法上简单地添加以下代码行来修复它。
[_picker selectRow:[_picker selectedRowInComponent:0] inComponent:0 animated:YES];
单击“保存”按钮时,您可以通过以下方式从组件中检索所选对象
[component1Array objectAtIndex:[myPicker selectedRowInComponent:1]];
在哪里,
并且“[myPicker selectedRowInComponent:2]”将从组件'2'中的myPicker 返回当前选择的原始编号。您可以通过以下方式从 component2 中检索所选对象
[component2Array objectAtIndex:[myPicker selectedRowInComponent:2]];
希望对你有效。
这可能是一个“黑客”,但它有效。我的情况略有不同。我正在使用 one UIPicker
,但可以更改它正在操作的对象。
我的问题是你可以选择一个对象,旋转选择器,当它完成旋转时,选择另一个对象。最终发生的是新对象正在获得该旋转的最终值,而不是被忽略。
我做了什么在设置时UIPicker
我会tag
根据它应该更新的对象给它一个唯一的。
- (void)updatePickerView:(UIPickerView*)pickerView
{
...
pickerView.tag = [self.myObject.position integerValue];
[pickerView selectRow:i inComponent:0 animated:NO];
}
然后当它完成旋转时,我检查以确保标签仍然排列。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView.tag == [self.myObject.position integerValue]) {
...
}
}
我不确定它在 Apple 方面是如何运作的,但这足以让它为我工作。
作为旁注,我不确定这是否重要,但我UIPicker
在一个UITableViewCell
但因为它是一个可重复使用的单元格并且因为我遇到了我遇到的问题,所以我假设 UIPicker 从它移出时是相同的一个位置到另一个位置(因为屏幕上一次只有一个位置)。
var isSpinning = false (Global)
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
self.isSpinning = true
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.isSpinning = false
}