ios - 如何使用具有多列的 UIpickerView 填充 TextField?

标签 ios xcode nsstring uitextfield uipickerview

我有一个带有 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!更改了代码,现在我收到错误..

[ session 于 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]];
 }

最佳答案

试试这个:

-(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

关于ios - 如何使用具有多列的 UIpickerView 填充 TextField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11753156/

相关文章:

ios - XCode iOS 模拟器是否需要互联网访问?

ios - 我的快速代码不模拟

ios - 项目从 git 编译但在 Xcode 6 中不是新的

objective-c - 如何从另一个 NSString 中删除 NSString 的文本

iphone - 将十六进制字符串转换为文本的 NSString?

ios - 在 iPhone/iPad 上获取 ARP 表总是返回 nil MAC

ios - 将值存储到分段控件的变量中

objective-c - 词法或预处理器问题

xcode - 开发 Flutter iOS 插件包 - Xcode 项目中的 Pods 文件夹为空

iphone - Markdown 到 NSAttributedString 库?