【问题标题】:How can I populate TextField with a UIpickerView that has more than one column?如何使用具有多于一列的 UIpickerView 填充 TextField?
【发布时间】:2012-08-01 05:42:00
【问题描述】:

我有一个 UIPickerView 显示与 textField 的 inputView。我已将pickerView 分成两列。一种是整数 50-500。另一个是 0.25,0.50,0.75。我希望他们能够选择整数,然后选择另一列的数字的小数部分。我不知道如何显示“完整”号码。当我移动第二列时,它只是将其注册为移动第一列并更改整数。我希望用户能够选择一个整数,数字的小数部分,点击完成,文本字段显示格式为“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]];
 }

【问题讨论】:

  • 你的 weightPickerView 是如何初始化的?
  • -(IBAction)weightFieldDidBeginEditing{ weightPickerView = [[UIPickerView alloc] init]; weightField.inputView = weightPickerView; weightPickerView.showsSelectionIndicator = YES; weightPickerView.delegate = self; weightPickerView.dataSource = 自我; [weightPickerView 发布]; } @里克
  • 好像您在 -(IBAction)weightFieldDidBeginEditing 中发布了 weightPickerView,并稍后尝试在“if([pickerView isEqual:weightPickerView])”中访问它。仅在使用完毕后才释放 weightPickerView(在从视图中删除 pickerview 之后)。

标签: ios xcode nsstring uitextfield uipickerview


【解决方案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”,则必须在将小数段附加到整数段之前执行一些字符串操作以删除前导零。看到这个post

【讨论】:

  • 如果您想使用数字进行数学计算,只需在代码中的任意位置使用:[weightField.text floatValue]。
  • 我使用了你上面给我的代码,用于pickerView didselect 行,但它不能完全正确地显示正确的数字。例如,使用选择器,我在第一行选择了 51,在第二行选择了 0.50,并且 textField 显示 51.50,然后如果我只将右列(小数)移动到 0.75,它会将 textField 更改为 52.75它应该是什么(51.75)。
  • 我明白了。这是因为在 weightField.text = [NSString stringWithFormat:@"%@%@",[weightPickerArray objectAtIndex:row], [weightPickerArray2 objectAtIndex:row]] 中使用了相同的“row”变量。您将需要使用不同的变量来记住第一列的行和第二列的另一个变量。
【解决方案2】:

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

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

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];
}

希望这会有所帮助。

【讨论】:

  • 我使用的是pickerView didSelectRow 方法,然后是textField.text = "(code)"。我用 didSelectRow 方法尝试了你的版本,我在输出中得到了非常奇怪的数字。我将分享我拥有的 pickerView 代码,并用于显示输出。请您看一下,看看您是否可以再次帮助我?我会用代码更新问题...@vitality
【解决方案3】:

您可以使用下面的答案,但是,如果您打算稍后在某些数学中使用这些值,从长远来看,使用 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]];
}

尝试类似的方法。

希望对你有帮助。

【讨论】:

  • 听取了您的建议并使用了 NSNumber,但是,现在当我运行应用程序并单击 weightField 时,我的应用程序崩溃了。错误将被发布。是的,我稍后会在一些数学计算中使用这些数字。 @活力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多