我想用UIPickerView来命名,但我就是看不出我该如何解决这个问题……
这个例子和我想要的很相似,而且很容易理解。我有两个简单的选择器,数字从0到10,然后我有一个文本字段,用户可以在其中插入一个数字。
我想要的是,当用户插入,例如,7,然后在其中一个选取器中选择一个数字,例如5,其他选取器移动到2,因此和是7。0和7,3和4也是如此。
基本上,如何让一个选取器移动,根据选择的另一个选取器值,基于之前插入的输入。
非常感谢:)
发布于 2013-05-27 19:43:24
试试看,假设你有两个UIPickerView,比如pickerview1和pickerview2,那么你可以像下面这样设置逻辑。
注意1.:-如果您使用不同的2 UIPickerView,则使用以下代码..
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (thePickerView == pickerview1) {
if ([yourTextfield.text integerValue] > row) {
int selectVal = [yourTextfield.text integerValue] - row ;
[pickerview2 selectRow:selectVal inComponent:0 animated:YES];
}
else{
int selectVal = row - [yourTextfield.text integerValue] ;
[pickerview2 selectRow:selectVal inComponent:0 animated:YES];
}
}
}如果您在UITextField 7中输入值,然后选择value 5 of pickerview1,则其row编号为4。
在这里,如果从7中减去4,则在设置或选择行号是3 of pickerview2之后,得到的是3,其值是e124 e225。
注释2.:-如果您使用了1个UIPickerView和2个组件,则使用以下代码..
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
if ([yourTextfield.text integerValue] > row) {
int selectVal = [yourTextfield.text integerValue] - row ;
[thePickerView selectRow:selectVal inComponent:1 animated:YES];
}
else{
int selectVal = row - [yourTextfield.text integerValue] ;
[thePickerView selectRow:selectVal inComponent:1 animated:YES];
}
}
}所以你得到了你想要的确切的输出..
我希望你收到了..。
享受编码的乐趣..:)
发布于 2013-05-27 19:46:00
添加数组并重新加载组件,如下所示:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
club=[[NSString alloc] initWithFormat:@"%@" , [Nations objectAtIndex:row]];
[secondcomponearray addObject club];
[pickerView reloadComponent:1];
}发布于 2013-05-27 20:35:40
从这篇文章修改的代码:find pair of numbers in array that add to given sum
下面的代码循环查找加起来等于number (输入到文本字段中的数字)的可能的对,然后在选择器中选择数字等于对中的一个数字的行。该代码假定您的数据源包含从0到某些总数的数字,并按升序排列。
NSInteger number = [[textField text] integerValue];
int i = 0;
int j = number-1
NSMutableArray *possiblePairs = [NSMutableArray array];
while(i < j){
if (i + j == number) {
possiblePairs[] = @[(i), @(j)];
}
else if (a[i] + a[j] < number) i += 1;
else if (a[i] + a[j] > number) j -= 1;
}
[picker1 selectRow:[dataSource indexOfObject:@(i)] inComponent:0];
[picker2 selectRow:[otherDataSource indexOfObject:@(j)] inComponent:0];https://stackoverflow.com/questions/16772084
复制相似问题