UIPopoverController的奇怪错误

时间:2012-03-26 03:59:51

标签: objective-c ios uipopovercontroller

我正在尝试使用UIImagePickerController从iPhone / iPad上的用户照片中抓取照片。这段代码适用于iPhone,但是当我在iPad上运行时,调试器给我的消息是“由于未捕获的异常终止应用程序'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.

}

1 个答案:

答案 0 :(得分:3)

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];