11

我正在尝试重置UIPickerViewon 按钮单击。我的 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];
}

知道我做错了什么吗?

谢谢

4

3 回答 3

32
[picker reloadAllComponents];
[picker selectRow:0 inComponent:0 animated:YES];
于 2012-06-25T14:22:50.480 回答
9

斯威夫特版本:

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

于 2016-05-05T20:59:27.417 回答
5
//use this line for going at the top of the label index 0:

[picker selectRow:0 inComponent:0 animated:YES];
于 2012-06-25T14:27:14.487 回答