ios 警告:尝试在已经呈现的 MainTableViewController 上呈现 ModalTableViewController (null)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25213152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: Attempt to present ModalTableViewController on MainTableViewController which is already presenting (null)
提问by derdida
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)
警告:尝试在 MainTableViewController... 上呈现 ModalTableViewController... 已经呈现(空)
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.
自定义 1x1px 按钮。用 segue 连接 popover。在单元格上点击移动按钮到单元格并打开弹出窗口。
So there is no code for opening the popover, only with storyboard editor.
所以没有打开popover的代码,只有storyboard editor。
I get the same error message (sometimes) just if I tap the same popover again.
如果我再次点击相同的弹出框,我会收到相同的错误消息(有时)。
回答by Steve
I had this issue because I was trying to perform segue / present from within:
我遇到这个问题是因为我试图从内部执行 segue/present:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
I changed it to:
我把它改成:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
and it fixed it!
它修复了它!
回答by Aace
I'm not on swift yet, but for Objective-C, I ended up wrapping the presentViewController call in a performSelector call.
我还没有使用 swift,但是对于 Objective-C,我最终将 presentViewController 调用包装在 performSelector 调用中。
-(void) present
{
[self performSelector: @selector(ShowModalTableViewController) withObject: nil afterDelay: 0];
}
-(void) ShowModalTableViewController
{
[self presentViewController: ctrlModalTableViewController animated: true completion: nil];
}
回答by Harris
This will work also:
这也将起作用:
dispatch_async(dispatch_get_main_queue(), ^ {
[self presentViewController:vc animated:YES completion:nil];
});
回答by Jeff
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.
如果可以,请尝试先关闭弹出框,然后再显示您的模态。
回答by Vaibhav Saran
This is a iOS 8issue on iPadactually and will not occur in iOSbelow 8. You can put a condition there:
这实际上是iPad上的iOS 8问题,在iOS 8以下的iOS中不会发生。您可以在那里设置条件:
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.
但请考虑以下 Steve 的观点(https://stackoverflow.com/a/26380194/362310)并更改此代码。
回答by Fadi
Easily just add this code snippet into ViewDidLoad()
of your main uiviewcontroller
只需将此代码片段添加到ViewDidLoad()
您的主 uiviewcontroller 中即可
definesPresentationContext = true
回答by Tim
This happens if a UIActionSheet is presented at the moment of new view controller presentation call, for example This code works for me
如果在新的视图控制器呈现调用时呈现 UIActionSheet,则会发生这种情况,例如此代码适用于我
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[controller presentViewController:modalViewController animated:YES completion:NULL];
}];
回答by zero0cool
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
对我来说,这发生在我有两个 UIBarButton 项目作为 NavigationItem 的一部分并且都有一个触发的 segue 来打开视图作为弹出窗口 - 使用它们自己的控制器。当点击另一个 BarButtonItem 时,一个弹出窗口不会自动关闭。然而,当我点击 popover 之外的其他地方时,它会消失。我最终覆盖了 UINavigationController 并添加了一个扩展版本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.
我相信这dispatch_async(dispatch_get_main_queue(), ^ {})
不是真正必要的,我只是将其添加为预防措施。
回答by Willy
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
.
就我而言,我不小心将我的按钮同时链接到 anIBAction
和Storyboard Segue
kind Present As Popover
。我为解决这个问题所做的是删除我按钮的Touch Up Inside
事件链接IBAction
并仅使用Storyboard Segue
.