与UIPopoverController奇怪的错误

我正尝试使用UIImagePickerController从iPhone / iPad上的用户照片中抓取照片。 这个代码适用于iPhone,但是当我在iPad上运行它时,debugging器给我提示消息“由于未捕获的exception'NSGenericException',原因:' – [UIPopoverController dealloc] popover仍然可见时到达”。 我对Objective-C非常陌生,所以我不确定是什么原因导致的,我不会释放任何东西,而且我已经打开了ARC。 这是我的代码:ViewController.m

#import "PhotoViewController.h" @implementation PhotoViewController @synthesize grabButton; @synthesize image; @synthesize imgPicker; - (IBAction)grabImage { if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker]; [popover presentPopoverFromRect:self.image.bounds inView:self.image permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } else { [self presentModalViewController:imgPicker animated:YES]; } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo { image.image = img; [[picker parentViewController] dismissModalViewControllerAnimated:YES]; } - (void)viewDidLoad { self.imgPicker = [[UIImagePickerController alloc] init]; self.imgPicker.allowsImageEditing = YES; self.imgPicker.delegate = self; self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } 

UIPopover是一个丑恶的拼凑物体。 它必须是一个强大的财产和一个有价证券,以确保Dealloc不被提前到达。 在.h中添加这个像这样:

 @interface MyClass: NSObject { UIPopover *_popover; } @property (nonatomic, strong) UIPopover * popover; //.m @synthesize popover = _popover; 

在实例化popup窗口时,将其分配给属性或实例:

 self.popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker]; 

要么

 _popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];