0

我正在尝试使 iPhone 应用程序在 iPad 上运行,但 UIPopoverController 出现错误。

- (IBAction)photoTapped1 {
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
// If in editing state, then display an image picker; if not, create and push a photo view controller.
    if (self.editing) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:YES completion:nil];
        [imagePicker release];
    } else {
        RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
        recipePhotoViewController.hidesBottomBarWhenPushed = YES;
        recipePhotoViewController.recipe = recipe;
        [self.navigationController pushViewController:recipePhotoViewController animated:YES];
        [recipePhotoViewController release];
    }
}else{
    if (self.editing){
        self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
        self.popover.delegate =self;
        [popover release];
    }else{
         RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
         recipePhotoViewController.hidesBottomBarWhenPushed = YES;
         recipePhotoViewController.recipe = recipe;
         [self.navigationController pushViewController:recipePhotoViewController animated:YES];
         [recipePhotoViewController release];
    }}}

我得到的错误是:'NSInvalidArgumentException',原因:'-[UIPopoverController initWithContentViewController:] 不能用nil.'调用。

任何可以帮助我处理此代码的人,我已经在互联网上查看了解决方案和示例,但似乎无法使其工作。

谢谢你。

___添加到原始问题_ ____

我在这里添加 recipePhotoViewController,我假设 iPhone 和 iPad 的 ImageView 操作是相同的。

我的.h文件

@class Recipe;

@interface RecipePhotoViewController : UIViewController {
@private
    Recipe *recipe;
    UIImageView *imageView;
}

@property(nonatomic, retain) Recipe *recipe;
@property(nonatomic, retain) UIImageView *imageView;

@end

这是我的 .m 文件

@implementation RecipePhotoViewController

@synthesize recipe;
@synthesize imageView;

- (void)loadView {
self.title = @"Photo";

imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight |      UIViewAutoresizingFlexibleWidth;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.backgroundColor = [UIColor blackColor];

self.view = imageView;   }
- (void)viewWillAppear:(BOOL)animated {
imageView.image = [recipe.image valueForKey:@"image"];}
- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation  {
 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[imageView release];
[recipe release];
[super dealloc];
}   @end
4

3 回答 3

1

您存储在变量中的弹出框的内容popover显然是nil. 我看不到它是在您提供的代码中创建的。

也许您打算将食谱照片控制器呈现为弹出框的内容。在这种情况下,你会做类似的事情

RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
self.popover = [[UIPopoverController alloc] initWithContentViewController:recipePhotoViewController];
于 2013-04-06T10:47:37.783 回答
1

您正在以错误的方式初始化弹出控制器:

self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];

您应该传递要在弹出框内显示的控制器 - 而不是弹出框本身(这是nil因为您尚未初始化它)!

也许这对你有用?

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];

如果这是正确的,那么您还应该呈现刚刚创建的弹出窗口:– presentPopoverFromRect:inView:permittedArrowDirections:animated:

——例如:

[self.popover presentPopoverFromRect:sender.frame inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

sender的论点在哪里:

- (IBAction)photoTapped1:(id)sender {
于 2013-04-06T10:47:58.107 回答
0

问题出在这行代码中,

    self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];

试试这样

    UIViewController* popoverContent = [[UIViewController alloc] init];
            UIView*   popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 400, 320, 260)];
        popoverContent.view = popoverView;
//Add what ever you want popoverView

 self.popover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
        self.popover.delegate =self;
于 2013-04-06T10:50:18.453 回答