【发布时间】: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