0

我在 Xcode 中有一个通用应用程序。如果用户使用的是 iPad,则使用库中的图像按钮效果很好。但是,如果他们使用 iPhone,则该按钮不起作用。

这是我收到的错误。

由于未捕获的异常“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;
    }
}
}
4

1 回答 1

0

发生此崩溃是因为您不能在 iPhone 上使用 UIPopOverControllers,而只能在 iPad 上使用。

[UIPopoverController initWithContentViewController:] 在 UIUserInterfaceIdiom Pad下运行时调用。

尝试这样的事情:

- (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];
    }
}
于 2012-09-06T16:12:57.987 回答