1

我有一个带有 textField 的 inputView 的 UIPickerView 显示。我已将pickerView 分成两列。一种是整数 50-500。另一个是 0.25,0.50,0.75。我希望他们能够选择整数,然后选择另一列的数字的小数部分。我不知道如何显示“完整”号码。当我移动第二列时,它只是将其注册为移动第一列并更改整数。我希望用户能够选择一个整数、数字的小数部分、点击完成,并且 textField 显示一个格式为“100.25”的数字或他们选择的任何数字。我已经对完成按钮进行了编码......并且整个数字仅以格式显示,即“100”。

谢谢!

更新!!!代码!!!!

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:  (NSInteger)component{
if ([pickerView isEqual:weightPickerView]) {
    weightField.text = [NSString stringWithFormat:@"%d.%d",[weightPickerArray   objectAtIndex:row], [weightPickerArray2 objectAtIndex:row]];



-(void)populateWeightPickerArray{
weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight <=500; weight++) {
    NSString *weightString = [NSString stringWithFormat:@"%d%",weight];
    [weightPickerArray addObject:weightString];
}
}

-(void)populateWeightPickerArray2{
weightPickerArray2 = [[NSMutableArray alloc] init];
[weightPickerArray2 addObject:@"0.25"];
[weightPickerArray2 addObject:@"0.50"];
[weightPickerArray2 addObject:@"0.75"];
}

所以我希望用户选择他们的体重——例如第一列为 100,第二列为 0.25。然后我希望文本字段显示 100.25...

更新 2!更改了代码,现在我收到一个错误..

[会话开始于 2012-08-03 10:39:39 -0500。] 2012-08-03 10:39:45.656 CalorieAppFinal[1088:207] -[NSCFNumber isEqualToString:]:无法识别的选择器发送到实例 0x4b336c0 2012-08 -03 10:39:45.661 CalorieAppFinal[1088:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSCFNumber isEqualToString:]:无法识别的选择器发送到实例 0x4b336c0”*在第一次抛出时调用堆栈

这是我如何初始化我的 weightPicker

 -(IBAction)weightFieldDidBeginEditing{
weightPickerView = [[UIPickerView alloc] init];
weightField.inputView = weightPickerView;
weightPickerView.showsSelectionIndicator = YES;
weightPickerView.delegate = self;
weightPickerView.dataSource = self;
[weightPickerView release];
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

if ([pickerView isEqual:weightPickerView]) {
    int valOne = [[weightPickerArray objectAtIndex:row] intValue];
    CGFloat valTwo = [[weightPickerArray2 objectAtIndex:row] floatValue];
    weightField.text = [NSString stringWithFormat:@"%d.%.2f",valOne, valTwo];
}


-(void)populateWeightPickerArray{
weightPickerArray = [[NSMutableArray alloc] init];
for (int weight = 50; weight <=500; weight++) {
    [weightPickerArray addObject:[NSNumber numberWithInt:weight]];
}
 }

-(void)populateWeightPickerArray2{
weightPickerArray2 = [[NSMutableArray alloc] init];
[weightPickerArray2 addObject:[NSNumber numberWithFloat:0.25f]];
[weightPickerArray2 addObject:[NSNumber numberWithFloat:0.50f]];
[weightPickerArray2 addObject:[NSNumber numberWithFloat:0.75f]];
 }
4

3 回答 3

1

**编辑为第一个答案有点令人困惑

在字符串中显示浮点数或十进制数时,有两种常用方法。

stringWithFormat
stringByAppendingFormat

因此,如果您有两个列/选择器触发两个单独的方法,或者如果您触发相同的方法但检查特定的选择器,您可以这样做:

- (void)somePickerDidChangeValueMethod:(UIPicker)picker
{
    //If you have on float value
    myTextField.text = [NSString stringWithFormat:@"%.2f",yourFloatValue];
            //the %.xf represents how many decimal places to show
    //If you have two int values
    myTextField.text = [NSString stringWithFormat:@"%d.%d",int1,int2];


    //If incoming number is a float
    myTextField.text = [myTextField.text stringByAppendingFormat:@".%.0f",yourFloatValue];
    //If incoming number is an int
    myTextField.text = [myTextField.text stringByAppendingFormat:@".%d",intVal];
}

希望这会有所帮助。

于 2012-08-01T05:47:33.217 回答
1

试试这个:

-(void)populateWeightPickerArray2
{
    weightPickerArray2 = [[NSMutableArray alloc] init];
    [weightPickerArray2 addObject:@".25"];  /* remove the leading zero */
    [weightPickerArray2 addObject:@".50"];  /* remove the leading zero */
    [weightPickerArray2 addObject:@".75"];  /* remove the leading zero */
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if ([pickerView isEqual:weightPickerView]) 
    {
        /* Updated the following line with selectedRowinComponent */
        weightField.text = [NSString stringWithFormat:@"%@%@",[weightPickerArray objectAtIndex:[weightPickerView selectedRowInComponent:0]], [weightPickerArray2 objectAtIndex:[weightPickerView selectedRowInComponent:1]]];
    }
}

要使用数字进行计算:

float number = [weightField.text floatValue];

如果要显示“0.25”、“0.50”和“0.75”,则必须在将小数段附加到整数段之前执行一些字符串操作以删除前导零。看到这个帖子

于 2012-08-02T03:25:24.480 回答
0

您可以使用下面的答案,但是,如果您打算稍后在某些数学中使用这些值,从长远来看,使用 NSNumber 可能更容易。由您决定-两者都可以。

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if ([pickerView isEqual:weightPickerView])
    {
        int valOne = [[weightPickerArray objectAtIndex:row] intValue];
        CGFloat valTwo = [[weightPickerArray2 objectAtIndex:row] floatValue];
        weightField.text = [NSString stringWithFormat:@"%d.%.2f",valOne,valTwo];
    }
}


-(void)populateWeightPickerArray
{
    weightPickerArray = [[NSMutableArray alloc] init];
    for (int weight = 50; weight <=500; weight++)
    {
        //easier to store NSNumberWithInt as opposed to a string
        [weightPickerArray addObject:[NSNumber numberWithInt:weight]];
    }
}

-(void)populateWeightPickerArray2
{
    weightPickerArray2 = [[NSMutableArray alloc] init];
    [weightPickerArray2 addObject:[NSNumber numberWithFloat:0.25f]];
    [weightPickerArray2 addObject:[NSNumber numberWithFloat:0.50f]];
    [weightPickerArray2 addObject:[NSNumber numberWithFloat:0.75f]];
}

尝试类似的东西。

希望能帮助到你。

于 2012-08-02T04:01:21.143 回答