我在UIPopOver中显示PickerView。
为此,我在按钮上单击使用以下代码来显示PopOver。
- (IBAction)btnSelectTeachers:(id)sender {
UIView *masterView;
CGRect pickerFrame ;
if(appDelegate.isOSVersionGreaterThan7)
{
masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 216)];
pickerFrame = CGRectMake(0, 0, 240, 216);
}
else
{
masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 216)];
pickerFrame = CGRectMake(0, 0, 300, 216);
}
UIPickerView *myPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
[myPicker setDataSource: self];
[myPicker setDelegate: self];
myPicker.tag=3;
[myPicker selectRow:selectedTeacherIndex inComponent:0 animated:NO];
[myPicker setShowsSelectionIndicator:YES];
self.myPickerView=myPicker;
[masterView addSubview:myPicker];
UIViewController *viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
viewController.view = masterView;
viewController.contentSizeForViewInPopover = viewController.view.frame.size;
self.popoverController =[[UIPopoverController alloc] initWithContentViewController:viewController];
UIButton *button = (UIButton *)sender;
[self.popoverController presentPopoverFromRect:button.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
[self.myPickerView reloadAllComponents];
/*if(selectedTeacherIndex<0)
{
selectedTeacherIndex=0;
}*/
[self.myPickerView selectRow:selectedTeacherIndex+1 inComponent:0 animated:NO];
[self pickerView:self.myPickerView didSelectRow:selectedTeacherIndex+1 inComponent:0];
}
大多数情况下它的工作正常,但有时会因上述异常而终止。