我有一个带有textField的inputView的UIPickerView显示。我已将pickerView拆分为两列。一个是整数50-500。另一个是0.25,0.50,0.75。我希望他们能够选择整数,然后用另一列选择数字的小数部分。我无法弄清楚如何显示“完整”号码。当我移动第二列时,它只是将其注册为移动第一列并更改整数。我希望用户能够选择整数,数字的小数部分,完成命中,textField显示格式为“100.25”的数字或他们选择的任何数字。我有完成按钮编码...并且整个数字只显示格式,即“100”。
谢谢!
UPDATE !!! CODE !!!!
-(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表示第1列,第2列表示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]];
}
答案 0 :(得分: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];
}
希望这有帮助。
答案 1 :(得分: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]];
}
尝试类似的东西。
希望它有所帮助。