我正在尝试重置UIPickerView
on 按钮单击。我的 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];
}
知道我做错了什么吗?
谢谢