我正在尝试让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操作是相同的。
my.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
答案 0 :(得分:1)
您存储在变量popover
中的popover的内容显然是nil
。我无法看到它在您提供的代码中创建。
也许您打算将食谱照片控制器作为弹出窗口的内容。在这种情况下,你会做类似
的事情RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
self.popover = [[UIPopoverController alloc] initWithContentViewController:recipePhotoViewController];
答案 1 :(得分:1)
您正在以错误的方式初始化弹出框控制器:
self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
你应该传递你想要在popover中显示的控制器 - 而不是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 {
答案 2 :(得分: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;