Warning: Attempt to present ModalTableViewController on MainTableViewController which is already presenting (null)

余生颓废 提交于 2019-11-28 06:09:48

I'm not on swift yet, but for Objective-C, I ended up wrapping the presentViewController call in a performSelector call.

-(void) present
{
    [self performSelector: @selector(ShowModalTableViewController) withObject: nil afterDelay: 0];
}

-(void) ShowModalTableViewController
{
    [self presentViewController: ctrlModalTableViewController animated: true completion: nil];
}

I had this issue because I was trying to perform segue / present from within:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

I changed it to:

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

and it fixed it!

Harris

This will work also:

dispatch_async(dispatch_get_main_queue(), ^ {
        [self presentViewController:vc animated:YES completion:nil];
});

It appears this is true of presenting anything over a popover. The reason all of the previous responses work is likely because whatever action is being taken happens before the popover (or activity sheet, or similar) is dismissed.

If you can, try dismissing the popover first, then presenting your modal.

Vaibhav Saran

This is a iOS 8 issue on iPad actually and will not occur in iOS below 8. You can put a condition there:

if ([controller respondsToSelector:@selector(popoverPresentationController)])
{
    // iOS8
    controller.popoverPresentationController.sourceView = self.view; // or any of your UIiew
}

But consider Steve's point also (https://stackoverflow.com/a/26380194/362310) below with this code change.

Easily just add this code snippet into ViewDidLoad() of your main uiviewcontroller

 definesPresentationContext = true

This happens if a UIActionSheet is presented at the moment of new view controller presentation call, for example This code works for me

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [controller presentViewController:modalViewController animated:YES completion:NULL];
    }];

For me, this happened when I had two UIBarButton items as part of a NavigationItem and both had a triggered segue to open views as popover - with their own controllers. One popover would not automatically dismiss when tapping the other BarButtonItem. It would however dismiss when I tapped elsewhere outside the popover. I ended up overriding UINavigationController and adding an extended version of presentViewController:animated:completion

/*
 * Workaround for apparent bug in iPad that popover does not automatically dismiss if another bar button item is pressed
 */

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {

    /*
     * Make sure this runs in the main queue
     */
    dispatch_async(dispatch_get_main_queue(), ^ {
        if (
            [self.presentedViewController isKindOfClass:[ViewController1 class]]
            || [self.presentedViewController isKindOfClass:[ViewController2 class]]
            || [self.presentedViewController isKindOfClass:[ViewController3 class]]
            || [self.presentedViewController isKindOfClass:[ViewController4 class]]
            ) {
            [self dismissViewControllerAnimated:YES completion:^{
                [super presentViewController:viewControllerToPresent animated:flag completion:completion];
            }];
        }

        else {
            [super presentViewController:viewControllerToPresent animated:flag completion:completion];
        }
    });
}

I believe the dispatch_async(dispatch_get_main_queue(), ^ {}) is not really necessary, I just added it as precaution.

In my case, I accidentally link my button to an IBAction and Storyboard Segue with kind Present As Popover at the same time. What I did to fix this was to remove my button's Touch Up Inside event link to IBAction and used only Storyboard Segue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!