如何使用双组件 UIPickerview 调用转换
Posted
技术标签:
【中文标题】如何使用双组件 UIPickerview 调用转换【英文标题】:how to call a conversion using double component UIPickerview 【发布时间】:2012-06-26 07:43:34 【问题描述】:我将在下面发布我的代码作为快速说明我在我的 .h 文件中定义了这两个组件,并且我需要在选择某些行时调用一个操作。例如,如果组件中的行 == 0 和其他组件 == 1 这样做? 还是 iPhone 编程的新手,在此先感谢您!!!
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
return 2;
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:
(NSInteger)component
if (component==kapickerComponent)
return [parentarray count];
if (component == kbpickerComponent)
return [subarray count];
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:
(NSInteger)rowforComponent:(NSInteger)component
if (component == kapickerComponent)
return [self.parentarray objectAtIndex:row];
if (component == kbpickerComponent)
return [self.subarray objectAtIndex:row];
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
if ( row. kapickerComponent ==0, and row.kbpickerComponent==1 ??????????? )
float number1 = ([initamount.text floatValue ]);
initamount.text = [[NSString alloc]initWithFormat:@" ", number1];
double answer = number1 *12;
result.text = [[NSString alloc]initWithFormat:@" ", answer];
【问题讨论】:
【参考方案1】:这里:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
if ([thePickerView selectedRowInComponent:kapickerComponent] == 0 && [thePickerView selectedRowInComponent:kbpickerComponent] == 1)
float number1 = [initamount.text floatValue];
initamount.text = [[NSString alloc]initWithFormat:@"%f", number1];
double answer = number1 *12;
result.text = [[NSString alloc] initWithFormat:@"%f", answer];
【讨论】:
好的,谢谢。我刚刚尝试过,结果出现错误或“成员引用基础 ype 'int' 不是结构或联合”有什么想法吗? kapickerComponent 是组件的整数,对吗?所以你需要 kapickerComponent 的第 0 行和 kbpickerComponent 的第 1 行? 是的,我正在做一个单位转换器,并让 kapickerComponent 填充了一个单位数组,并且与 kbpickerComponenet 相同,因此当两者的某一行被调用时,我需要编写一个语句来进行计算。 pickerView:didSelectRow:inComponent: 每次用户选择一行时只调用一次。因此,您需要保存选定的选项,或者使用 [thePickerView selectedRowInComponent: kapickerComponent] == 0。我将编辑我的帖子以使用它【参考方案2】:if ( kapickerComponent.row == 0 && kbpickerComponent.row ==1)//你的代码在这里
【讨论】:
谢谢,但它有一个错误“成员引用基 ype 'int' 不是结构或联合”我似乎无法弄清楚以上是关于如何使用双组件 UIPickerview 调用转换的主要内容,如果未能解决你的问题,请参考以下文章