自定义 UIPickerView - 限制用户一次旋转一个轮子 [英] custom UIPickerView - limit user to spinning one wheel at a time

查看:20
本文介绍了自定义 UIPickerView - 限制用户一次旋转一个轮子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的 UIPickerView,其中包含三个相互依赖的组件(即,第二个组件显示的值取决于第一个组件中选择的内容,第三个组件取决于第一个组件)和第二).我从 NSDictionary 中获取 UIPickerView 中显示的值.

I have a custom UIPickerView with three components that are dependent on one another (i.e., the second one shows values depending on what has been selected in the first one and the third is dependent on the first and second). I am getting the values that are shown in the UIPickerView out of an NSDictionary.

一切都很好,除了当我同时旋转两个组件时,应用程序有时会崩溃(没有时间重新加载数据).这是我的 pickerView:didSelectRow:inComponent 的样子:

Everything works nicely, except, when I spin two of the components at the same time, the app sometimes crashes (no time to reload data). This is what my pickerView:didSelectRow:inComponent looks like:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
    NSLog(@"Selecting row %d component %d", row, component);

    // When A is changed, we need to reload B and C
    if (component == 0) {

        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];

        // need to reload the C after reloading B
        [self pickerView:pickerView didSelectRow:0 inComponent:1];

    }
    else if (component == 1) {
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }
    [self updateSelection];
}

有没有办法防止用户一次旋转多个拾取器组件以防止崩溃?

Is there a way to prevent the user from spinning more than one component of the picker at a time to prevent a crash?

谢谢!

推荐答案

天哪,你递归调用didSelectRow:component:

我知道您处理参数的方式会阻止它深入两层以上,但仍然不要那样做.曾经.

I know that the way you handle parameters prevents it from going more than two levels deep, but still, don't do that. Ever.

没有办法阻止用户一次旋转多个组件.多点触控和 iOS 的乐趣在于让一切都运转起来,并看着它们全部稳定下来,进入一致的状态.由于微调组件需要时间来稳定,用户只需用一根手指就可以将它们全部设置在推特上.您必须学会相应地编写代码.

There's no way to prevent the user from spinning more than one component at a time. It is the joy of multitouch and iOS to set everything in motion and watch it all settle down into a consistent state. And because the spinner components take time to settle, the user can set them all atwitter using just one finger. You must learn to write your code accordingly.

在这种情况下,您希望从自身内部删除对 didSelectRow:component: 的所有调用 - 它由系统只调用.您使用 reloadComponent: 是在正确的轨道上,只是您没有重新加载足够的它们:

In this case, you want to delete all calls to didSelectRow:component: from within itself - it is to be called by the system only. You're on the right track with your use of reloadComponent:, it's just that you're not reloading enough of them:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"Selecting row %d component %d", row, component);

    [self updateSelection];

    // When A is changed, we need to reload B and C
    if (component == 0) {
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
    } else if (component == 1) {
        [pickerView reloadComponent:2];
    }
}

我不知道您的 updateSelection 做了什么 - 大概它根据 pickerView 的值设置数据模型.这很好 - 但它需要发生在你的方法的顶部,这样当你的 titleForRowviewForRow 中的任何一个被调用时,它都有适当的数据可供选择.此外,从这三个(updatetitleForviewFor)中的任何一个中,不要 将值断言到选择器中!只需阅读它.

I don't know what your updateSelection does - presumably it sets the data model based on the value of pickerView. This is fine - but it needs to happen at the top of your method, so that when whichever of your titleForRow or viewForRow is called, it has the proper data to choose from. Also, from within any of those three (update,titleFor, viewFor) do not assert values into the picker! Just read from it.

我认为这对您来说是一个很好的起点,但是随着您离目标越来越近,您将不得不在这里和那里稍微调整一下.我想你会发现 Pickers 的关键是它们只在组件微调器之一确定一个值时才断言 didSelectRow:component: ,并且它们真的很擅长继续旋转,如果你重新加载它们.试一试,看看你是否能取得一些进展,如果你卡住了,请回信.

I think this is a good starting point for you, but you will have to tweak things a little here and there as you get closer to your goal. I think you will find that the key to Pickers is that they only assert didSelectRow:component: when one of the component spinner things settles down on a value, and they're really good about continuing to spin if you reload them. Give this a shot and see if you can make some progress, send word back if you get stuck.

这篇关于自定义 UIPickerView - 限制用户一次旋转一个轮子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆