尝试加载UIImagePicker时出现popoverView错误

时间:2014-03-15 01:23:18

标签: ios objective-c uiviewcontroller uiimagepickercontroller uipopovercontroller

我正在尝试创建弹出式摄像头视图但是我收到此错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController _commonPresentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'

我正在调用我的cameraViewController

- (void) cameraButtonSelected
{
    CameraViewController *cameraViewController = [[CameraViewController alloc] init];
    cameraViewController.view.frame = CGRectMake(100.0, 100.0, 200.0, 150.0);
    [self.view addSubview:cameraViewController.view];
}

这是我试图创建的ViewController viewDidLoad方法

- (void)viewDidLoad
{
    [super viewDidLoad];

    if(self.view.window!=nil)
        [objPopView presentPopoverFromRect:self.view.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;

    objPopView = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
    [objPopView presentPopoverFromRect:CGRectMake(842, 163, 0, 0)
                                inView:self.view
              permittedArrowDirections:UIPopoverArrowDirectionRight
                              animated:YES];
}

1 个答案:

答案 0 :(得分:3)

我看到两个问题。

  • 你这么做太早了。 viewDidLoad表示您的视图在窗口中。它只是意味着视图存在。等到viewWillAppear:viewDidAppear:执行此操作。这是视图现在在界面中的标志。您可能必须使用条件才能在第一次执行此操作。

  • 这段代码非常奇怪:

    if(self.view.window!=nil)
        [objPopView presentPopoverFromRect:self.view.frame inView:self.view
             permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    

    您尚未定义objPopView,如果满足此条件,您将执行弹出窗口的此演示,然后直接进行另一个弹出窗口的演示。