【问题标题】:How to reset UIPickerView to index:0,iPhone如何将 UIPickerView 重置为索引:0,iPhone
【发布时间】:2012-06-25 14:17:24
【问题描述】:

我正在尝试在单击按钮时重置UIPickerView。 我的 pickerview id 是在运行时创建的,我已经设置了代表。 谷歌搜索后,我发现了

[pickerView reloadAllComponents];

但这会让我的应用程序每次到达这里都会崩溃。

索引 0 处的对象是“从列表中选择”,然后是项目。 单击提交按钮时,我希望“从列表中选择”应保留在我的标签顶部(选定索引:0)。

这是我的代码

ViewDidload

 pickerView = [[UIPickerView alloc] init];
    pickerView.delegate = self;
    pickerView.dataSource = self;

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


 // Total rows in our component.
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        return [nameArray count];
    }

// Display each row's data.
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [nameArray objectAtIndex: row];
    }

// Do something with the selected row.

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        dlbl.hidden=YES;
        NSLog(@"You selected this: %@", [nameArray objectAtIndex: row]);
        [btnSelectDesigner setTitle:[nameArray objectAtIndex: row] forState:UIControlStateNormal]; 

    }

并且,点击按钮:

-(IBAction)btnSubmitClicked:(id)sender{
[pickerView reloadAllComponents];
}

知道我做错了什么吗?

谢谢

【问题讨论】:

  • 一些代码有助于更好地调试。

标签: iphone ipad uipickerview


【解决方案1】:
[picker reloadAllComponents];
[picker selectRow:0 inComponent:0 animated:YES];

【讨论】:

  • 你能解释一下使用 reloadAllComponents 的意义吗?在此先感谢:)
  • @AnthoPak 不知道为什么我需要在 6 年前调用该方法,但它不应该是必要的
  • 哈哈哈经典!
【解决方案2】:

Swift 版本:

picker.selectRow(0, inComponent: 0, animated: true)

【讨论】:

    【解决方案3】:
    //use this line for going at the top of the label index 0:
    
    [picker selectRow:0 inComponent:0 animated:YES];
    

    【讨论】:

    • 点击按钮?让我崩溃
    • 你的数组分配了吗??它是第一次正常工作吗??..您可以在这里显示您的崩溃日志..到底发生了什么..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 2019-09-16
    • 2015-06-21
    相关资源
    最近更新 更多