ios - iPad 的 UIPopoverController "must not be called with ` nil`"错误

标签 ios ipad uipopovercontroller

我正在尝试让 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,我假设 ImageView 操作对于 iPhone 和 iPad 是相同的。

我的.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

最佳答案

您正在以错误的方式初始化弹出框 Controller :

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

你应该传递你想在弹出窗口内显示的 Controller ——而不是弹出窗口本身(它是 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 {

关于ios - iPad 的 UIPopoverController "must not be called with ` nil`"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15849882/

相关文章:

iphone - 将日期和时间组合成单个 NSDate 的问题

iphone - 无法从现有源代码创建新的静态库

ios - NSEntityDescription 仅在 iPad Air 7.1 上返回 nil

ios - 关闭弹出 View Controller

ios - 推送通知无法获取设备 token

objective-c - 是提供一个 NSMutableArray 还是从中创建一个 NSArray 更好?

iphone - UITableView 复选标记计数

ios - 从父类继承属性

ios - 如何手动设置 UIDocumentInteractionController 呈现的选项菜单的箭头方向?

objective-c - 无法调整UIPopupController显示图像