UIPicker - 初始值问题

发布于 2024-09-24 19:51:13 字数 3747 浏览 1 评论 0原文

我有一个 UIPicker,其中包含 3 个包含数值的组件,这允许用户以 hh:mm:ss 格式设置时间。如果我从第一个组件中选择小时,我的 UITextField 看起来像这样:10:(null):(null)。当用户选择适当的 mm 和 ss 时,可以删除 (null)。但如果用户只想输入 10 小时,我希望其他值为 00 而不是 (null),而用户无需物理移动选取器组件。

有人对此有什么想法吗?

下面是一些代码,这个问题与timePicker有关。

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if([pickerView isEqual: routePicker])
    {
        route.text = [[routeArray objectAtIndex:0] valueForKey:@"route_name"];
        return [[routeArray objectAtIndex:row] valueForKey:@"route_name"];
    }
    else if([pickerView isEqual: activityPicker])
    {
        activity.text = [activityArray objectAtIndex:0];
        return [activityArray objectAtIndex:row];
    }
    else if([pickerView isEqual: intensityPicker])
    {
        if ([intensityArray objectAtIndex:row]==@"Low")
            numberIntensity=2;
        else if ([intensityArray objectAtIndex:row]==@"Low-Medium")
            numberIntensity=3.5;
        else if ([intensityArray objectAtIndex:row]==@"Medium")
            numberIntensity=5;
        else if ([intensityArray objectAtIndex:row]==@"Medium-High")
            numberIntensity=6.5;
        else if ([intensityArray objectAtIndex:row]==@"High")
            numberIntensity=8;
        else numberIntensity=0;

        intensity.text = [intensityArray objectAtIndex:0];
        return [intensityArray objectAtIndex:row];
    }
    else if([pickerView isEqual: timePicker])
    {
        switch (component)
        {
            case 0:
                return [hourArray objectAtIndex:row];
                break;
            case 1:
                return [minuteArray objectAtIndex:row];
                break;
            case 2:
                return [secondArray objectAtIndex:row];
                break;
            default:
                return 0;
                break;
        }
    }
    else if([pickerView isEqual: distancePicker])
    {
        switch (component)
        {
            case 0:
                return [distance1Array objectAtIndex:row];
                break;
            case 1:
                return [distance2Array objectAtIndex:row];
                break;
            default:
                return 0;
                break;
        }
    }   
    else 
    {
        return 0;
    }
}



-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if([pickerView isEqual: routePicker])
    {
        route.text = [[routeArray objectAtIndex:row] valueForKey:@"route_name"];
    } 
    else if([pickerView isEqual: timePicker])
    {
        if (component == 0)
        {
            selectedHour = [hourArray objectAtIndex:row];
        } 
        else if (component == 1)
        {
            selectedMinute = [minuteArray objectAtIndex:row];
        } 
        else if (component == 2)
        {
            selectedSecond = [secondArray objectAtIndex:row];
        }
        time.text = [NSString stringWithFormat:@"%@:%@:%@", selectedHour, selectedMinute, selectedSecond];
    }
    else if([pickerView isEqual: distancePicker])
    {
        if (component == 0)
        {
            selectedDistance1 = [distance1Array objectAtIndex:row];
        } 
        else if (component == 1)
        {
            selectedDistance2 = [minuteArray objectAtIndex:row];
        } 
        distance.text = [NSString stringWithFormat:@"%@.%@", selectedDistance1, selectedDistance2];
    }
    else if([pickerView isEqual: activityPicker])
    {
        activity.text = [activityArray objectAtIndex:row];
    }
    else if([pickerView isEqual: intensityPicker])
    {
        intensity.text = [intensityArray objectAtIndex:row];
    }
}

I have a UIPicker with 3 components containing numeric values, this allows the user to set the time in hh:mm:ss format. If I select the hour from the first component my UITextField looks like this: 10:(null):(null). The (null) can be removed when the user selects the appropriate mm and ss. But if the user just wants to enter 10 hours, I want the other values to be 00 instead of (null), without the user having to physically move the picker components.

Anyone have any ideas on this ?

Some code below, this question is related to the timePicker.

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if([pickerView isEqual: routePicker])
    {
        route.text = [[routeArray objectAtIndex:0] valueForKey:@"route_name"];
        return [[routeArray objectAtIndex:row] valueForKey:@"route_name"];
    }
    else if([pickerView isEqual: activityPicker])
    {
        activity.text = [activityArray objectAtIndex:0];
        return [activityArray objectAtIndex:row];
    }
    else if([pickerView isEqual: intensityPicker])
    {
        if ([intensityArray objectAtIndex:row]==@"Low")
            numberIntensity=2;
        else if ([intensityArray objectAtIndex:row]==@"Low-Medium")
            numberIntensity=3.5;
        else if ([intensityArray objectAtIndex:row]==@"Medium")
            numberIntensity=5;
        else if ([intensityArray objectAtIndex:row]==@"Medium-High")
            numberIntensity=6.5;
        else if ([intensityArray objectAtIndex:row]==@"High")
            numberIntensity=8;
        else numberIntensity=0;

        intensity.text = [intensityArray objectAtIndex:0];
        return [intensityArray objectAtIndex:row];
    }
    else if([pickerView isEqual: timePicker])
    {
        switch (component)
        {
            case 0:
                return [hourArray objectAtIndex:row];
                break;
            case 1:
                return [minuteArray objectAtIndex:row];
                break;
            case 2:
                return [secondArray objectAtIndex:row];
                break;
            default:
                return 0;
                break;
        }
    }
    else if([pickerView isEqual: distancePicker])
    {
        switch (component)
        {
            case 0:
                return [distance1Array objectAtIndex:row];
                break;
            case 1:
                return [distance2Array objectAtIndex:row];
                break;
            default:
                return 0;
                break;
        }
    }   
    else 
    {
        return 0;
    }
}



-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if([pickerView isEqual: routePicker])
    {
        route.text = [[routeArray objectAtIndex:row] valueForKey:@"route_name"];
    } 
    else if([pickerView isEqual: timePicker])
    {
        if (component == 0)
        {
            selectedHour = [hourArray objectAtIndex:row];
        } 
        else if (component == 1)
        {
            selectedMinute = [minuteArray objectAtIndex:row];
        } 
        else if (component == 2)
        {
            selectedSecond = [secondArray objectAtIndex:row];
        }
        time.text = [NSString stringWithFormat:@"%@:%@:%@", selectedHour, selectedMinute, selectedSecond];
    }
    else if([pickerView isEqual: distancePicker])
    {
        if (component == 0)
        {
            selectedDistance1 = [distance1Array objectAtIndex:row];
        } 
        else if (component == 1)
        {
            selectedDistance2 = [minuteArray objectAtIndex:row];
        } 
        distance.text = [NSString stringWithFormat:@"%@.%@", selectedDistance1, selectedDistance2];
    }
    else if([pickerView isEqual: activityPicker])
    {
        activity.text = [activityArray objectAtIndex:row];
    }
    else if([pickerView isEqual: intensityPicker])
    {
        intensity.text = [intensityArray objectAtIndex:row];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千年*琉璃梦 2024-10-01 19:51:13

我认为这一行:

time.text = [NSString stringWithFormat:@"%@:%@:%@", selectedHour, selectedMinute, selectedSecond];

可以更改为:

time.text = [NSString stringWithFormat:@"%@:%@:%@",
             (selectedHour ? selectedHour : @"00"),
             (selectedMinute ? selectedMinute : @"00"),
             (selectedSecond ? selectedSecond : @"00")];

这假设 selectedHourselectedMinuteselectedSecond 最初都是 nil在此代码之外。我远离我的Mac,所以我没有测试过。

或者,您可以将 selectedHourselectedMinuteselectedSecond 初始化为 @"00"

I think this line:

time.text = [NSString stringWithFormat:@"%@:%@:%@", selectedHour, selectedMinute, selectedSecond];

could be changed to:

time.text = [NSString stringWithFormat:@"%@:%@:%@",
             (selectedHour ? selectedHour : @"00"),
             (selectedMinute ? selectedMinute : @"00"),
             (selectedSecond ? selectedSecond : @"00")];

This assumes that selectedHour, selectedMinute and selectedSecond are all initially nil outside of this code. I am away from my mac, so I haven't tested this.

Alternatively, you could initialize selectedHour, selectedMinute and selectedSecond to @"00".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文