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

前端 未结 13 1285
逝去的感伤
逝去的感伤 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 09:11

    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.

提交回复
热议问题