Question

I am trying to create a popup camera view however I am getting this error

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.'

I am calling my cameraViewController like this

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

And this is the ViewController viewDidLoad method I have tried to create

- (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];
}
Was it helpful?

Solution

I see two problems.

  • You are doing this too early. viewDidLoad means your view is not yet in the window. It just means that the view exists. Wait until viewWillAppear: or viewDidAppear: to do this. That is the sign that the view is now in the interface. You might have to use a condition so as to do it only the first time.

  • This code is really weird:

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

    You have not yet defined objPopView, plus if this condition is met you will do this presentation of the popover and then go right on and do another presentation of the popover.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top