我需要创建两个下拉列表(选择器视图)并检索用户在两个下拉列表中选择的值并对它们执行计算?是否可以在 Xcode 中创建下拉列表?
问问题
126 次
1 回答
2
Maybe try a two-component picker view.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
Say you have two arrays, self.firstArray and self.secondArray to populate the two components.
- (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;
}
}
Then you can get to the user selected values like this:
- (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]]);
}
于 2013-02-16T14:55:15.413 回答