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

36,040

Solution 1

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

Solution 2

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!

Solution 3

This will work also:

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

Solution 4

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.

Solution 5

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

 definesPresentationContext = true
Share:
36,040
derdida
Author by

derdida

Software Developer with Experience in PHP, Swift, Objective C, Javascript, C#.

Updated on July 09, 2022

Comments

  • derdida
    derdida almost 2 years

    I have a problem with a popover. If I tap on a cell I will load a popover to select more details. Everything works fine, but when I press my cell again I receive every time the following message:

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

    If I tap on another cell I will not get this Warning. Only if a tap the same row again.

    I tried lots of things but I am not able to solve this problem. I load my popover with like this:

    var popover: UIPopoverController!
    var popoverContent: ModalTableViewController!
    

    and on my cell tap:

    popoverContent = self.storyboard.instantiateViewControllerWithIdentifier("ModalTableViewController") as ModalTableViewController
    
    popoverContent.selectedQuestionID = indexPath!.row               
    popover = UIPopoverController(contentViewController: popoverContent)
    popover.delegate = self
    
    popover.presentPopoverFromRect(currentCell.LabelCellTitle.frame, inView: currentCell.LabelCellTitle.superview, permittedArrowDirections: UIPopoverArrowDirection.Left, animated: true)
    

    And to dismiss

    func popoverControllerDidDismissPopover(popoverController: UIPopoverController!) {
    
        popover.dismissPopoverAnimated(false) // just to check
    
        self.popover = nil
        self.popoverContent = nil
    
    }
    

    Any ideas?

    Edit:

    If I check with:

    if(self.popoverContent == nil) {
    

    before opening it, I'll find out that it's not nil when I tap the same cell again.

    Edit again:

    I have the same problem if I create it with a little different setup:

    Custom 1x1px Button. Connect popover with segue. On cell tap move button to cell and open popover.

    So there is no code for opening the popover, only with storyboard editor.

    I get the same error message (sometimes) just if I tap the same popover again.

  • Travis M.
    Travis M. almost 10 years
    Not sure why this was down-voted. This actually fixed the issue for me.
  • Josip B.
    Josip B. over 9 years
    I don't know what is happening with iOS but I'm glad there are people like @Acace which saved my day!
  • lihudi
    lihudi over 9 years
    I had an app that worked perfectly in IOS7 but had this issue in IOS8. This tip fixed it.
  • Zhang
    Zhang over 9 years
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ /* put code to execute here */ }); also fixed it on iOS 8 for me, in case you don't want to create a new method just to call the same code.
  • MartinR
    MartinR over 9 years
    This was the case for me. Simple solution to a dumb issue.
  • Paludis
    Paludis over 9 years
    This fixed my issue as well.
  • Thiru
    Thiru over 9 years
    If in case you are presenting from within ActionSheet button click - try @Steve's answer for using didDismissWithButtonIndex
  • Erik Engheim
    Erik Engheim over 9 years
    This solved a similar problem for me. However I don't understand why this works. Why do I need to do this? Any links to anything that will give more background info?
  • Elijah
    Elijah over 9 years
    It runs presentViewController on a different thread, so there is no longer a conflict. As to why - because Apple must have changed the way they are handling these things behind the scenes.
  • Erik Engheim
    Erik Engheim over 9 years
    Different thread? I am not following. dispatch_get_main_queue() should give you the main UI thread right, which is what I am calling this from.
  • Elijah
    Elijah over 9 years
    Yes, it is the main thread, but it's being dispatched asynchronously, so it's not preventing the other activivities from happening. Here is some discussion on GCD jeffreysambells.com/2013/03/01/…
  • Vipin Johney
    Vipin Johney over 9 years
    Does this solution cause any other issue which needs to be handled?? Just asked
  • Steve
    Steve over 9 years
    Not that I have come across.
  • Daniel
    Daniel over 9 years
    The above does WORK AROUND the problem/bug in iOS 8. But it's a hack way to do it. Steve's answer to switch to using didDismissWithButtonIndex: is the better solution. LOOK AT THE STEVE'S ANSWER. It's less work and much less fragile.
  • Julian
    Julian about 9 years
    I think the solution you proposed might work but has to be consider as a nasty hack. it is not solving the source of the problem which is showing pop up before dismissing the previous one. Make sure you have dismissed the old one and then present the new one. It works as it adds a change to the next run on the main run loop
  • Martin
    Martin about 9 years
    This should be the correct answer. Currently accepted is a hack, this one is clever solution.
  • aryaxt
    aryaxt about 9 years
    Here is another solution and explanation on why this is happening. stackoverflow.com/questions/24942282/…
  • nikhil.thakkar
    nikhil.thakkar over 8 years
    Just for the records, if you have already defined a segue in storyboard on tap of a button or cell tap and you fire the segue programmatically as well, then this can cause the said error.
  • Raj Tandel
    Raj Tandel about 8 years
    This fixed the issue for me. but what difference does it make if we use clickedButtonAtIndex or didDismissWithButtonIndex ? can anyone please explain.
  • Steve
    Steve about 8 years
    @RajTandel Been a while but I think clickedButtonAtIndex might not be called on the main thread (why most other answer suggest dispatching back to main queue).
  • Mantas Laurinavičius
    Mantas Laurinavičius almost 8 years
    UiActionSheet is of UIPopoverController class and prevents another pop over view to be pushed until it is removed. I knew it was the case and this answer is what I am looking for.
  • Mihir Oza
    Mihir Oza over 7 years
    I got issue in iPad but after use your code it worked for me in IOS 9.
  • AndyH
    AndyH over 7 years
    I have tried every possible solution, but this was the only one that worked. Thanks
  • Deeper
    Deeper almost 7 years
    thanks so much, it saved my day, i found this issue happened on my iPad but iPhone are no problem at all