Popovers cannot be presented from a view which does not have a window

前端 未结 13 1186
逝去的感伤
逝去的感伤 2020-11-30 08:22

What does this error indicate:

\"Popovers cannot be presented from a view which does not have a window.\"
相关标签:
13条回答
  • 2020-11-30 08:48

    I replaced

    [actionSheet showFromBarButtonItem:self.navigationController.navigationItem.leftBarButtonItem animated:YES];
    

    with

    [actionSheet showInView:self.view];
    
    0 讨论(0)
  • 2020-11-30 08:50

    yes, you are right but still we can add subview from parent class in it. so it can be represented from a view which have a window:

    [popoverController.contentViewController.view addSubview:mySubView];
    
    0 讨论(0)
  • 2020-11-30 08:51

    There will be a view from which you asks to display your popover.The reason for this error is because you didn't made this view as a subview of the window.

     [self.view addSubview:displayPopOverVC];
    

    where displayPopOverVC is the view controller from which the popOver appears

    0 讨论(0)
  • 2020-11-30 08:59

    i had the same problem, after adding PresentPopOver in viewDidAppear this was solved

    - (void) viewDidAppear:(BOOL)animated{
         CGRect popoverRect = screenBounds;         
         popoverRect.size.width = MIN(popoverRect.size.width,0) ;
         popoverRect.origin.x  = screenBounds.origin.x;
    
         [popoverController
         presentPopoverFromRect:popoverRect
         inView:self.view
         permittedArrowDirections:UIPopoverArrowDirectionAny
         animated:YES];
    }
    

    this was happening as inView:self.view should be called after viewDidLoad as suggested by @hey68You and MobiMaciek..

    0 讨论(0)
  • 2020-11-30 09:03

    There are many ways to get to this error. Basically you need to wait to call the presentPopover command until your calling view is added to a window. I did it this way.

    - (void)viewDidAppear:(BOOL)animated
    {
        [self methodThatDisplaysPopOver];
    }
    

    My presentPopoverFromRect call is inside my methodThatDisplaysPopOver function.

    You could protect every presentPopover call like MobiMaciek suggests with this.

    if (self.view.window != nil)
        [popoverController presentPopoverFromRect:CGRectMake(10, 10, 100, 100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    

    However, I think it would be better to understand when self.view.window gets assigned and make sure that you present you popover after the view has a window.

    0 讨论(0)
  • 2020-11-30 09:07

    Just encountered this issue. Turned out that the inView: parameter was using an IBOutlet that wasn't connected in IB. Thus, an attempt was made to launch the popover in nil. That doesn't work.

    So, make sure you are using a valid view.

    0 讨论(0)
提交回复
热议问题