在 uitextfield 中输入来自 3 个 UIPickers 的分数?

发布于 2024-11-09 12:45:04 字数 2922 浏览 0 评论 0原文

所以我想做的不是输入小数,而是希望能够在文本字段中输入整数和分数。或者从 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 有它自己的选择器,英寸有它自己的选择器,分数有它自己的选择器,如何让所有三个在一个文本字段中显示为“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;
}

So what I'm intending to do is instead of typing in decimals, I want to be able to enter whole numbers and fractions in a textfield. Or select them from a UIPicker.

Example: Feet, Inches, fractions.(2 - 4 1/2).

What's the best way to accomplish this? Please be detailed in your answer. Thank you in advance!

Update: Ok maybe a little more detail is needed.

I'm going to use the picker, and want to display 5 rows like this:

<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).

Ok, So I was able to get 3 pickers to display on screen just how I wanted, my biggest issue is getting it to display in the textfield properly. So just how the example is above, ft has it's own picker, inches has it's own, and fractions has it's own, How would you get all three to display as "00ft 00 0/0in" in one text field?
UPDATE: the code below is correct, make sure you create an IBOutlet for your UIView that contains the pickers, and hook it up in IB.

.m file

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙气飘飘 2024-11-16 12:45:04

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

By setting up UIPickerView object as the inputView, you won't need to worry about text input. Tapping on the text field would bring up the picker and make the input constrained just as you want it. You can either subclass or set them directly in your controller. I set it up as a subclass here. Its a rough implementation of what you need as I understand it. You can take a look and see if it helps.

风柔一江水 2024-11-16 12:45:04

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

有两种方法可以将分数放入 UIPicker 中,使用 pickerView:titleForRow:forComponent: UIPicker 委托的方法,以字符串形式给出分数。您可以通过此函数使用 UTF8 分数字符,例如 ¼。

如果没有提供足够的灵活性。您可以使用 pickerView:viewForRow:forComponent:reusingView: 委托方法。

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

I would advice to use a picker, it's way easier then typing fractions and will prevent the input of incorrect fractions.

There are two ways to get the fractions into your UIPicker, use the pickerView:titleForRow:forComponent: method of your UIPicker delegate to give the fraction as a string. You can use UTF8 fraction characters like ¼ with this function.

If is not giving enough flexibility. You could make images of the fractions and put the images into the picker by using the pickerView:viewForRow:forComponent:reusingView: method of your delegate.

The documentation will give you lots of information! Good luck!

娇俏 2024-11-16 12:45:04

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

It sounds like you want something like the date picker with multiple wheels to choose each part of the distance. Unfortunately there isn't a way to do this. You have 2 options though. The simple option is to use several pickers next to each other. It wont look quite the same but it's simple. The more complicated option is to build one with UIScrollViews and style it to look the way you want it too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文