【问题标题】:Enter fractions in a uitextfield from 3 UIPickers?在 3 UIPickers 的 uitextfield 中输入分数?
【发布时间】:2011-08-31 06:17:40
【问题描述】:

所以我打算做的不是输入小数,而是希望能够在文本字段中输入整数和分数。或者从 UIPicker 中选择它们。

示例:英尺、英寸、分数。(2 - 4 1/2)。

实现此目的的最佳方法是什么?请在您的回答中详细说明。提前谢谢!

更新:好的,也许需要更多细节。

我将使用选择器,并希望像这样显示 5 行:

<Ft>    <In>
0 0  S  0 0  S  1/16
1 1  P  1 1  P  1/8
2 2  A  2 2  A  3/16
3 3  C  3 3  C  1/4 
4 4  E  4 4  E  5/16 
ect...all the way to 9, and to 15/16 for the fractions. (Note the space between Ft and Inches and title for row).

好的,所以我能够让 3 个选择器按照我想要的方式在屏幕上显示,我最大的问题是让它正确显示在文本字段中。所以上面的例子是怎样的,ft 有它自己的选择器,inches 有它自己的,fractions 有它自己的,你如何让这三个在一个文本字段中显示为“00ft 00 0/0in”? 更新:下面的代码是正确的,确保为包含选择器的 UIView 创建一个 IBOutlet,并将其连接到 IB 中。

.m 文件

feetArray = [[NSMutableArray alloc] init];
for(int feet = 0; feet <= 9; feet ++){ 
    NSString *feetString = [NSString stringWithFormat:@"%d%", feet];
    [feetArray addObject:feetString]; // Add the string.
}    

inchArray = [[NSMutableArray alloc] init];
for(int inch = 0; inch <= 12; inch ++){ 
    NSString *inchString = [NSString stringWithFormat:@"%d%", inch];
    [inchArray addObject:inchString]; // Add the string.
}

fractionArray = [[NSMutableArray alloc] init];
for(int frac = 0; frac <= 15; frac ++){ 
    NSString *fractionString = [NSString stringWithFormat:@"%d/16", frac];
    [fractionArray addObject:fractionString]; // Add the string.
}

}


//How many rows in each Picker.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
if (pickerView.tag == 1) {
    return 2;
}
if (pickerView.tag == 2) {
    return 2;
}

return 1;
}

//Selects array for each picker.
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 

if (pickerView.tag == 1) {

    return [feetArray count];
}
if (pickerView.tag == 2) {

    return [inchArray count];
}

return [fractionArray count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 

if (pickerView.tag == 1) {
    return [feetArray objectAtIndex:row];
}
if (pickerView.tag == 2) {
    return [inchArray objectAtIndex:row];
}
    return [fractionArray objectAtIndex:row];
}

//Formats the textfield based on the pickers.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

NSString *result = [feetArray objectAtIndex:[feetPicker selectedRowInComponent:0]];

result = [result stringByAppendingFormat:@"%@ft", [feetArray objectAtIndex:[feetPicker selectedRowInComponent:1]]];
result = [result stringByAppendingFormat:@" %@", [inchArray objectAtIndex:[inchesPicker selectedRowInComponent:0]]];
result = [result stringByAppendingFormat:@"%@", [inchArray objectAtIndex:[inchesPicker selectedRowInComponent:1]]];
result = [result stringByAppendingFormat:@" %@in", [fractionArray objectAtIndex:[fractionPicker selectedRowInComponent:0]]];

RiseTextField.text = result;
}

【问题讨论】:

    标签: objective-c xcode keyboard uitextfield uipickerview


    【解决方案1】:

    通过将UIPickerView 对象设置为inputView,您无需担心文本输入。点击文本字段会调出选择器并根据您的需要限制输入。您可以子类化或直接在控制器中设置它们。我将它设置为子类here。据我了解,它是您需要的粗略实现。你可以看看有没有帮助。

    【讨论】:

    • 看起来在您的示例中您有 1 个 3 行的选择器?所以我不需要 3 个不同的选择器?
    • 当你可以用一个来做的时候,你为什么要这样做。选择 3 有什么具体原因吗?
    • 是的,有人告诉我不能只用一个。哈哈,我知道必须有。
    • 老实说,我喜欢使用 3 个选择器,因为我可以看到英尺和英寸之间的分离效果,您能否帮助使用 3 个选择器进行格式化?
    • 您的示例得到“UIViewController 可能无法响应 initwithframe”警告..​​.有什么想法吗?
    【解决方案2】:

    我建议使用选择器,它比键入分数更容易,并且可以防止输入不正确的分数。

    有两种方法可以将分数放入 UIPicker,使用 UIPicker 委托的 pickerView:titleForRow:forComponent: 方法将分数作为字符串提供。您可以在此函数中使用 ¼ 等 UTF8 小数字符。

    如果没有提供足够的灵活性。您可以使用委托的pickerView:viewForRow:forComponent:reusingView: 方法制作分数的图像并将图像放入选择器中。

    文档将为您提供大量信息!祝你好运!

    【讨论】:

    • 你能再看看我的问题吗?我已经发布了我的代码,希望这会有所帮助。谢谢!
    【解决方案3】:

    听起来你想要一个带有多个轮子的日期选择器来选择距离的每个部分。不幸的是,没有办法做到这一点。不过,您有 2 个选择。一个简单的选择是使用几个相邻的选择器。它看起来不太一样,但很简单。更复杂的选择是使用 UIScrollViews 构建一个,并将其样式设置为您想要的样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多