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

IpadUipopovercontroller

Ipad Problem Overview


What does this error indicate:

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

Ipad Solutions


Solution 1 - Ipad

the thing that saved my life:

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

by adding if condition it doesn´t crash anymore. I don´t really get it because the presentPopoverFromRect function is ALWAYS called. There is no situation where window would be nil but anyway it did the trick.

edit: I have this code in viewDidAppear. Nevertheless in most cases it's enough to move presentPopoverFromRect to viewDidAppear or didMoveToWindow but in my case for some reason the if condition was necessary.

Solution 2 - Ipad

the view you're adding the popover to has to already have been added to a window with the "addSubview:" method.

Try waiting until

- (void) didMoveToWindow

is called for the view and then load the popover

Solution 3 - Ipad

I got this problem.

I had a UITabBarController as the detail view, and I set the barButtonItem as the leftBarButtonItem on all three navigation controllers in the tab bar.

vcChart.navigationItem.leftBarButtonItem = barButtonItem;
vcAnalysis.navigationItem.leftBarButtonItem = barButtonItem;
vcTechnicals.navigationItem.leftBarButtonItem = barButtonItem;

Turns out only the last one added is valid, and the previous two would throw the exception when tapped on.

To fix it, I only set the leftBarButtonItem for the visible view controller, and just switched the barButtonItem to the visible view controller every time the user switched tabs.

Solution 4 - Ipad

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.

Solution 5 - Ipad

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.

Solution 6 - Ipad

I received the same error message when assigning the same UIBarButtonItem to multiple navigation items as did Lewis. My example was slightly more complicated as I was using a UISplitViewController.

In my RootViewController I have an array of arrays to accomplish multiple sections within my table. Each time that the user clicks a row in the table, a new "detail" view controller is placed in the right pane of my splitViewController. Prior to setting the leftBarButtonItem = nil, I would receive a segfault after 3-4 clicks of the "Menu" button with the same error as a111. I updated my code to actually retrieve the previous detail view controller and set the leftBarButtonItem item to nil.

allData is my NSMutableArray that contains several other NSMutableArrays as objects.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Retrieve the new detail view controller
    UIViewController *detailViewController = [[self.allData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    // Add the detail view controller to a navigation controller and set the bar style
    UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
    detailNavigationController.navigationBar.barStyle = [[NSUserDefaults standardUserDefaults] integerForKey:@"UIBarStyle"];

    // Retrieve previous detail view controller and remove the leftBarButtonItem
    UINavigationController *previousDetailNavigationController = [splitViewController.viewControllers objectAtIndex:1];
    UIViewController *previousDetailViewController = [[previousDetailNavigationController viewControllers] lastObject];
    previousDetailViewController.navigationItem.leftBarButtonItem = nil;

    // Update the split view controller's view controllers array.
    NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailNavigationController, nil];
    splitViewController.viewControllers = viewControllers;

    [detailNavigationController release];
    [viewControllers release];

    // Dismiss the popover if it's present.
    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
    }

    // This sets the left bar to nil when in landscape and equal to "Menu" when in portrait.
    // We need to remove rootPopoverButtonItem from the previous viewController...
    detailViewController.navigationItem.leftBarButtonItem = rootPopoverButtonItem;
} 

The error message was slightly deceiving at first but the answers above helped me out. I wonder why I could click the "Menu" button up to 3-4 different times before the segfault... I'll investigate further.

Solution 7 - Ipad

This error also occurred when the inView: Parameter is incorrect - to test try self.view

Solution 8 - Ipad

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];

Solution 9 - Ipad

I had the same error message as the OP, in a very similar situation to that reported by TPoschel, except I had a split view controller with an embedded tab bar controller in the detail pane, and a navigation controller within this. The bar button item is added as the navigation bar leftBarButtonItem.

Only on iOS5.0 (not 5.1) does it seem to require you invalidate the bar button item on the tab bar you are leaving by setting it to nil. Before then adding the bar button to the navigation bar on the tab you are going to.

If I don't do that, from debugging my own code, the window property of the bar button item stays set to nil, and causes the exception, on returning to a screen you'd previously been to. I'm guessing as a side effect of setting the leftBarButtonItem in the navigation item, it goes off and sets the frame. But it doesn't seem to bother unless the button is different from what is currently set there. Hence, the need to set it to nil when leaving a tab, even though it is technically the same button that's being passed around.

I would upvote TPoschel's answer, except SO won't let me.

Solution 10 - Ipad

I had a problem like this. Received this message when clicking a customized UIBarButton item that invoked a selector method with did performSeque.

The problem was my segue was still attached to the UIBarButton item. It should have been attached to the main view of of the view controller. Changed this and worked fine.

P.S., all this got started because I wanted to add and "info" button to my UIToolBar. This isn't one in the system provided list and should be.

Solution 11 - Ipad

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

Solution 12 - Ipad

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

Solution 13 - Ipad

I replaced

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

with

[actionSheet showInView:self.view];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiona111View Question on Stackoverflow
Solution 1 - IpadMobile DeveloperView Answer on Stackoverflow
Solution 2 - Ipadhey68youView Answer on Stackoverflow
Solution 3 - IpadLewis AndersonView Answer on Stackoverflow
Solution 4 - Ipaduser161814View Answer on Stackoverflow
Solution 5 - IpadJoshua DanceView Answer on Stackoverflow
Solution 6 - IpadTPoschelView Answer on Stackoverflow
Solution 7 - IpadJohn GoodstadtView Answer on Stackoverflow
Solution 8 - IpadSteveView Answer on Stackoverflow
Solution 9 - IpadRob GlasseyView Answer on Stackoverflow
Solution 10 - IpadcaprijoeView Answer on Stackoverflow
Solution 11 - IpadMouzmiSadiqView Answer on Stackoverflow
Solution 12 - IpadiMeMyselfView Answer on Stackoverflow
Solution 13 - IpadRose PerroneView Answer on Stackoverflow