gpt4 book ai didi

iphone - 如何从picker列表中获取一个值并进行计算?

转载 作者:行者123 更新时间:2023-11-28 22:39:44 24 4
gpt4 key购买 nike

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

最佳答案

或许可以试试双组件选择器 View 。

- (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]]);
}

关于iphone - 如何从picker列表中获取一个值并进行计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14911095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com