UIPopoverController 的奇怪错误 [英] Strange Error With UIPopoverController
问题描述
我正在尝试使用 UIImagePickerController 从用户的 iPhone/iPad 上的照片中获取照片.这段代码适用于 iPhone,但是当我在 iPad 上运行它时,调试器给我消息由于未捕获的异常‘NSGenericException’而终止应用程序,原因:‘-[UIPopoverController dealloc]到达而弹出窗口仍然可见.".我对 Objective-C 很陌生,所以我不确定是什么原因造成的,我没有释放任何东西并且我打开了 ARC.这是我的代码:视图控制器.m
I'm trying to use UIImagePickerController to grab a photo from the users Photos on their iPhone / iPad. This code works just fine for iPhone, but when I run it on iPad, the debugger gives me the message "Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.". I'm very new to Objective-C, so I'm unsure of whats causing this, I do not dealloc anything and I have ARC turned on. Here is my code: 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 中,如下所示:
UIPopover is an ugly patchwork beast of an object. It must be a strong property and an iVar to ensure that Dealloc isn't reached prematurely. Add this in the .h Like so:
@interface MyClass: NSObject {
UIPopover *_popover;
}
@property (nonatomic, strong) UIPopover * popover;
//.m
@synthesize popover = _popover;
当您实例化弹出框时,将其分配给属性或实例:
When you instantiate the popover, assign it to either the property or the instance:
self.popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
或
_popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
这篇关于UIPopoverController 的奇怪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!