我正在尝试使用UIImagePickerController从用户在其iPhone / iPad上的照片中抓取照片。该代码在iPhone上运行良好,但是当我在iPad上运行时,调试器将显示消息“由于未捕获的异常'NSGenericException'而终止应用程序,原因:'-[UIPopoverController dealloc]在弹出窗口仍然可见的情况下到达。”。我对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是物体的丑陋拼凑而成的野兽。它必须是强大的属性,并且必须是iVar,以确保不会过早达到Dealloc。像这样将其添加到.h中:
@interface MyClass: NSObject {
UIPopover *_popover;
}
@property (nonatomic, strong) UIPopover * popover;
//.m
@synthesize popover = _popover;
实例化弹出窗口时,请将其分配给属性或实例:
self.popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
要么
_popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
关于objective-c - UIPopoverController的奇怪错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9866436/