2012-03-26 53 views
1

我試圖使用UIImagePickerController從iPhone/iPad上的用戶照片抓取照片。這段代碼適用於iPhone,但是當我在iPad上運行它時,調試器給我提示消息「由於未捕獲的異常'NSGenericException'導致終止應用程序,原因:' - [UIPopoverController dealloc]在popover仍然可見時到達」。我對Objective-C非常陌生,所以我不確定導致這種情況的原因,我不會釋放任何東西,並且我已經打開了ARC。這裏是我的代碼: ViewController.mUIPopoverController奇怪的錯誤

#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. 

} 
+0

可能的欺騙:HTTP ://stackoverflow.com/questions/8895071/uipopovercontroller-dealloc-reached-while-popover-is-still-visible – CodaFi 2012-03-26 04:10:31

+0

我在發帖之前看了一下,很不幸沒有幫助。 – 2012-03-26 04:14:39

回答

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

非常感謝!我應該刪除哪一類?對不起,我是編程新手。另外,我添加了上面發佈的所有更改,但同樣的錯誤仍在發生。 – 2012-03-26 04:32:03

+0

當您添加屬性或iVars(就像我們上面所做的那樣)時,您必須刪除名稱衝突的任何iVars,否則它們會遮蓋您在.h中聲明的iVar。只需從.m文件中刪除任何提及的UIPopover(單詞,而不是對象)即可。 – CodaFi 2012-03-26 04:44:55

+0

真棒,再次感謝! – 2012-03-26 04:47:37