首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据用户输入更改UiPickerView值

问根据用户输入更改UiPickerView值
EN

Stack Overflow用户
提问于 2013-05-27 19:23:07
回答 3查看 1.3K关注 0票数 2

我想用UIPickerView来命名,但我就是看不出我该如何解决这个问题……

这个例子和我想要的很相似,而且很容易理解。我有两个简单的选择器,数字从0到10,然后我有一个文本字段,用户可以在其中插入一个数字。

我想要的是,当用户插入,例如,7,然后在其中一个选取器中选择一个数字,例如5,其他选取器移动到2,因此和是7。0和7,3和4也是如此。

基本上,如何让一个选取器移动,根据选择的另一个选取器值,基于之前插入的输入。

非常感谢:)

EN

回答 3

Stack Overflow用户

发布于 2013-05-27 19:43:24

试试看,假设你有两个UIPickerView,比如pickerview1和pickerview2,那么你可以像下面这样设置逻辑。

注意1.:-如果您使用不同的2 UIPickerView,则使用以下代码..

代码语言:javascript
复制
- (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个组件,则使用以下代码..

代码语言:javascript
复制
   - (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];
            }
        }  
    }

所以你得到了你想要的确切的输出..

我希望你收到了..。

享受编码的乐趣..:)

票数 0
EN

Stack Overflow用户

发布于 2013-05-27 19:46:00

添加数组并重新加载组件,如下所示:

代码语言:javascript
复制
- (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];

}
票数 0
EN

Stack Overflow用户

发布于 2013-05-27 20:35:40

从这篇文章修改的代码:find pair of numbers in array that add to given sum

下面的代码循环查找加起来等于number (输入到文本字段中的数字)的可能的对,然后在选择器中选择数字等于对中的一个数字的行。该代码假定您的数据源包含从0到某些总数的数字,并按升序排列。

代码语言:javascript
复制
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];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16772084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档