我正在尝试重置点按钮上的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];
}
知道我做错了吗?
由于
答案 0 :(得分:29)
[picker reloadAllComponents];
[picker selectRow:0 inComponent:0 animated:YES];
答案 1 :(得分:6)
Swift版本:
picker.selectRow(0, inComponent: 0, animated: true)
答案 2 :(得分:5)
//use this line for going at the top of the label index 0:
[picker selectRow:0 inComponent:0 animated:YES];