2012-09-06 74 views
0

我在Xcode中有一個通用應用程序。如果用戶正在使用iPad,則來自庫按鈕的使用圖像效果很好。但是,如果他們使用iPhone的按鈕不起作用。Universal Xcode Camera Roll應用程序問題

這是我收到的錯誤。

由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:' - [UIPopoverController initWithContentViewController:]在UIUserInterfaceIdiomPad下未運行時調用。

被告知此代碼將工作。它是否進入下面的(IBAction)代碼?

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { Add Popover code here } else {  
Add alternative for popover here } 

- (IBAction) useCameraRoll: (id)sender 
{ 

if ([self.popoverController isPopoverVisible]) { 
    [self.popoverController dismissPopoverAnimated:YES]; 

} else { 
    if ([UIImagePickerController isSourceTypeAvailable: 
     UIImagePickerControllerSourceTypeSavedPhotosAlbum]) 
    { 
     UIImagePickerController *imagePicker = 
     [[UIImagePickerController alloc] init]; 
     imagePicker.delegate = self; 
     imagePicker.sourceType = 
     UIImagePickerControllerSourceTypePhotoLibrary; 
     imagePicker.mediaTypes = [NSArray arrayWithObjects: 
            (NSString *) kUTTypeImage, 
            nil]; 
     imagePicker.allowsEditing = YES; 

     newMedia = NO; 
    } 
} 
} 

回答

0

由於您不能在iPhones上使用UIPopOverControllers,因此只會在iPad上發生此次崩潰。

[UIPopoverController initWithContentViewController:]稱爲「當 下UIUserInterfaceIdiom 運行。

嘗試這樣:

- (IBAction)useCameraRoll:(id)sender 
{ 
    UIPopoverController *popoverController = [[UIPopoverController alloc] init]; 
    if ([UIImagePickerController isSourceTypeAvailable: 
     UIImagePickerControllerSourceTypeSavedPhotosAlbum]) 
    { 
     UIImagePickerController *imagePicker = 
     [[UIImagePickerController alloc] init]; 
     imagePicker.delegate = self; 
     imagePicker.sourceType = 
     UIImagePickerControllerSourceTypePhotoLibrary; 
     imagePicker.mediaTypes = [NSArray arrayWithObjects: 
            (NSString *) kUTTypeImage, 
            nil]; 
     imagePicker.allowsEditing = YES; 

     newMedia = NO; 
    } 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
     if (popoverController.isPopoverVisible) { 
      [popoverController dismissPopoverAnimated:YES]; 
     }else{ 
       [popoverController setContentViewController:imagePicker]; 
       [popoverController setDelegate:self]; 
       [popoverController presentPopoverFromRect:CGRectMake(0, 0, 320, 320) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
     } 
    }else{ 
     [imagePicker presentViewController:imagePicker animated:YES completion:nil]; 
    } 
} 
+0

好了,這樣的代碼給了我一些警告和錯誤。警告是popoverController隱藏實例變量的局部聲明。錯誤是使用未聲明的標識符圖像選擇器。 –

+0

@ Niche'AdMarketing我知道這些,我離開他們的唯一原因是,你會知道你必須移動一些東西,比如「imagePicker」的聲明。由於它包含在'isSourceTypeAvailable'「if」中,因此它不能在其外部使用。在你的頭文件中聲明imagePicker應該使這個靜音。 –

+0

在.h中聲明imagePicker它看起來像這樣嗎? UIImageView * imagePicker;然後@property(nonatomic,retain)IBOutlet UIImageView * imagePicker; –