IOS如何在UIPickerView中的项目点击时关闭UIPickerView
我有一个UIPickerView,当UIPickerView中选择一个项目时,我用UIToolBar和UIBarButtonItem关闭了UIPickerView。但是这不起作用,因为我发现你不能在自定义视图中执行此操作。IOS如何在UIPickerView中的项目点击时关闭UIPickerView
所以只是想问我该如何解决这个问题?
下面是代码,当我在一个自定义的UIView设立UIPickerView:在pickerView
选择一个项目将是后
-(void)setupPickerView:(NSDictionary*)dic{
NSDictionary *category = [dic objectForKey:@"category"];
pickerViewData = [[NSMutableArray alloc] init];
NSString *c_ID;
NSString *c_Name;
for(NSDictionary *dict in category)
{
if((NSNull *)[dict objectForKey:@"C_ID"] != [NSNull null]){
c_ID = [dict objectForKey:@"C_ID"];
}
if((NSNull *)[dict objectForKey:@"C_Name"] != [NSNull null]){
c_Name = [dict objectForKey:@"C_Name"];
}
[pickerViewData addObject:c_Name];
}
pickerViewCatergory = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 140, 320, 300)];
pickerViewCatergory.delegate = self;
pickerViewCatergory.dataSource = self;
pickerViewCatergory.showsSelectionIndicator = YES;
//pickerView.hidden = YES;
[pickerViewCatergory setBackgroundColor:[UIColor whiteColor]];
[pickerViewCatergory reloadAllComponents];
[self.viewCreateThread addSubview:pickerViewCatergory];
}
根据您的逻辑,基本的方式来摆脱pickerView
的善用UIPickerViewDelegate
方法-didSelectRow:inComponent:
例子:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//store selected row index or relevant data
//...
//then get rid of pickerView
[pickerView removeFromSuperview]; //simple way to just get rid of it
}
我已经尝试过了,但不是我想要它如何工作 – redoc01 2014-09-06 18:29:13
@ redoc01:呃...这是要做的地方。然而,如果你指定了更多关于如何显示'pickerView'的信息,那么它是层次结构的东西,那么我或其他人可以更有帮助。无论如何...你如何想从第一个地方的'UIToolbar'中隐藏'pickerView'? – staticVoidMan 2014-09-06 18:38:26
向我们提供样本项目,使我们ç看看发生了什么,我认为在视图层次结构中必定存在逻辑错误。 – Jageen 2014-09-06 19:23:09