如何从选择器列表中获取值并执行计算?

时间:2013-02-16 14:00:42

标签: iphone ios xcode4.2

我需要创建两个下拉列表(选择器视图)并在两个下拉列表中检索用户选择的值并对它们执行计算? 是否可以在Xcode中创建下拉列表?

1 个答案:

答案 0 :(得分:2)

也许尝试双组件选择器视图。

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}

假设你有两个数组,self.firstArray和self.secondArray来填充这两个组件。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row    forComponent:(NSInteger)component reusingView:(UIView *)view
{
if (component == 0) {
    UILabel *numberLabel = [[UILabel alloc] init];
    NSString *numberString = [self.firstArray objectAtIndex:row];
    numberLabel.font = [UIFont systemFontOfSize:14];
    numberLabel.text = numberString;
    return numberLabel;
} else {
    UILabel *alphabetLabel = [[UILabel alloc] init];
    NSString *alphabet = [self.secondArray objectAtIndex:row];
    alphabetLabel.font = [UIFont systemFontOfSize:14];
    alphabetLabel.text = alphabet;
    return alphabetLabel;
}
}

然后你可以得到用户选择的值,如下所示:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:   (NSInteger)component
{
NSLog(@"selected number: %@", self.firstArray[[self.pickerView selectedRowInComponent:0]]);
NSLog(@"selected alphabet: %@", self.secondArray[[self.pickerView selectedRowInComponent:1]]);
}