我正在尝试在 UIActionSheet 中添加 UIPicker 视图!选择器的委托和数据源设置正确!(我在没有操作表的情况下单独测试了它)但是当操作表显示在视图中时,pickerview 是可怕的黑框!我搜索了问题,我听说 UIPickerView 的数据源和委托设置不正确!我认为选择器视图不会调用它的方法,当它在一个UIActionsheet内浮现!所以我强迫选择器重新加载其所有组件将操作表
编辑:我进行了更正,代码完美运行!
- (void) presentPicker{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select some value" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
actionSheet.tag = AS_VIEW_TAG;
UIPickerView* picker = nil;
picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
picker.tag = 90;
picker.dataSource = self;
picker.delegate = self;
[picker setShowsSelectionIndicator:YES];
[actionSheet addSubview:picker];
[actionSheet showInView:self.view];
[actionSheet setFrame:CGRectMake(0, 50, 320, 380)];
}
- (void) willPresentActionSheet:(UIActionSheet *)actionSheet{
if(actionSheet.tag == AS_VIEW_TAG){
UIPickerView* picker = [actionSheet viewWithTag:PICKER_TAG];
//I force reload
[picker reloadAllComponents];
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:1] setFrame:CGRectMake(20, 266, 280, 46)];
[[subviews objectAtIndex:2] setFrame:CGRectMake(20, 317, 280, 46)];
}
}
UIPicker 委托/数据源方法
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
if (pickerView.tag == 90) {
return 1;
}
else{
return 0;
}
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(pickerView.tag == 90){
return 20;
}
else{
return 0;
}
}
- (UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
label.text = [NSString stringWithFormat:@"Value %d", ];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:17];
return label;
}
这是结果: