ios 警告:尝试在已经呈现的 * 上呈现 * (null)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32696615/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:45:53  来源:igfitidea点击:

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

iosswiftuitableviewuiviewcontrolleruistoryboardsegue

提问by Splendf

This is my first application for iOS.

这是我的第一个 iOS 应用程序。

So I have a UIVIewControllerwith a UITableViewwhere I have integrated a UISearchBarand a UISearchControllerin order to filter TableCells to display

所以我有一个UIVIewControllerUITableView在那里我有一个整合UISearchBarUISearchController以过滤TableCells到显示器

override func viewDidLoad() {
    menuBar.delegate = self
    table.dataSource = self
    table.delegate = self
    let nib = UINib(nibName: "ItemCellTableViewCell", bundle: nil)
    table.registerNib(nib, forCellReuseIdentifier: "Cell")

    let searchButton = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "search:")
    menuBar.topItem?.leftBarButtonItem = searchButton
    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        return controller
    })()
    self.table.reloadData()
}

I am using also a modal segue in order to open the element's ViewControllerwhere I will display details of the element.

我还使用了一个模态 segue 来打开元素ViewController,我将在其中显示元素的详细信息。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.index = indexPath.row
    self.performSegueWithIdentifier("ItemDetailFromHome", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "ItemDetailFromHome") {
        let settingsVC = segue.destinationViewController as! ItemDetailViewController
        settingsVC.parent = self
        if self.isSearching == true  && self.searchText != nil && self.searchText != ""  {
            settingsVC.item = self.filteredItems[self.index!]
        } else {
            settingsVC.item = self.items[self.index!]
        }

    }
}

That works fine until I try to display the ItemDetailViewControllerfor a filtered element (through the UISearchController).

这工作正常,直到我尝试显示ItemDetailViewController过滤元素(通过UISearchController)。

I have the following message :

我有以下消息:

Warning: Attempt to present <ItemDetailViewController: *>  on <HomeViewController: *> which is already presenting (null)

At every time I am going to the ItemDetailViewController.viewDidLoad()function but after that when the search is activated I have the previous error.

每次我要使用该ItemDetailViewController.viewDidLoad()功能时,但在激活搜索之后,我都会遇到以前的错误。

Any idea ? I have tried to use the following async dispatch but without success

任何的想法 ?我尝试使用以下异步调度但没有成功

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.index = indexPath.row
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.performSegueWithIdentifier("ItemDetailFromHome", sender: self)
    })
}

回答by Splendf

I have found out a solution.

我找到了解决办法。

I have add the following code in HomeViewController.viewDidLoadand that works !

我已经添加了以下代码HomeViewController.viewDidLoad并且有效!

definesPresentationContext = true

回答by grahamparks

In my case, I found my code to present the new viewController (a UIAlertController) was being called twice.

就我而言,我发现我的代码显示新的 viewController (a UIAlertController) 被调用了两次。

Check this before messing about with definesPresentationContext.

在搞乱之前检查一下definesPresentationContext

回答by Pavel Kataykin

In my case, I tried too early to show the new UIViewController before closing the previous one. The problem was solved through a call with a slight delay:

就我而言,在关闭上一个之前,我尝试过早地显示新的 UIViewController。通过稍有延迟的调用解决了问题:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
     self.callMethod()
}

回答by Lahiru Pinto

I got the same issue when i tried to present a VC which called inside the SideMenu(jonkykong).

当我尝试展示在SideMenu(jonkykong)内部调用的 VC 时,我遇到了同样的问题。

first i tried inside the SideMenu and i called it from the delegate to the MainVC both had the same issue.

首先我在 SideMenu 内部尝试,我从代理调用它到 MainVC 都遇到了同样的问题。

Solution:dismiss the SideMenu first and present the new VC after will works perfectly!.

解决方案:首先关闭 SideMenu 并在完美运行后呈现新的 VC!。

回答by Hiram Elguezabal

The problem for me is that I was presenting two modals and I have to dismiss both and then execute some code in the parent window ... and then I have this error... I solved it seting this code in the dismiss o the last modal presented:

我的问题是我正在呈现两个模态,我必须关闭它们,然后在父窗口中执行一些代码......然后我有这个错误......我解决了它在最后一次关闭中设置此代码模态呈现:

self.dismiss(animated: true, completion: {
                self.delegate?.callingDelegate()
            })

in other words instead of just dismiss two times .. in the completion block of the first dismiss call delegate that will execute the second dismiss.

换句话说,而不是仅仅解雇两次......在第一个解雇调用委托的完成块中,将执行第二次解雇。

回答by Brandon A

This happened with me on our project. I was presenting our log in/log out ViewControlleras a pop-over. But whenever I tried to log back out again and display the pop-over again, I was getting this logged out in my console:

这发生在我的项目中。我将我们的登录/注销显示ViewController为弹出窗口。但是每当我尝试再次注销并再次显示弹出窗口时,我都会在控制台中注销它:

Warning: Attempt to present UIViewController on <MY_HOME_VIEW_CONTROLLER> which is already presenting (null)

Warning: Attempt to present UIViewController on <MY_HOME_VIEW_CONTROLLER> which is already presenting (null)

My guess is that the pop-over was still being held by my ViewControllereven though it was not visible.

我的猜测是弹出窗口仍然由我持有,ViewController即使它不可见。

However you are attempting to display the new ViewController, the following code I used to solve the issue should work for you:

但是,您尝试显示 new ViewController,我用来解决问题的以下代码应该适合您:

func showLoginForm() {

    // Dismiss the Old
    if let presented = self.presentedViewController {
        presented.removeFromParentViewController()
    }

    // Present the New
    let storyboard = UIStoryboard(name: "MPTLogin", bundle: Bundle(for: MPTLogin.self))
    let loginVC = storyboard.instantiateViewController(withIdentifier: "LogInViewController") as? MPTLogInViewController
    let loginNav = MPTLoginNav(rootViewController: loginVC!)
    loginNav.modalPresentationStyle = .pageSheet;
    self.present(loginNav, animated: true, completion: nil)
}

回答by Clay Ellis

In my case I was trying to present a UIAlertControllerat some point in the app's lifetime after using a UISearchControllerin the same UINavigationController.

在我的情况下,我试图UIAlertController在应用程序生命周期中的某个时间点在UISearchController同一UINavigationController.

I wasn't using the UISearchControllercorrectly and forgot to set searchController.isActive = falsebefore dismissing. Later on in the app I tried to present the alert but the search controller, though not visible at the time, was still controlling the presentation context.

我没有UISearchController正确使用并且searchController.isActive = false在解雇之前忘记设置。后来在应用程序中,我尝试呈现警报,但搜索控制器虽然当时不可见,但仍在控制呈现上下文。

回答by Prasanna Kumar

I faced the same kind of problem What I did is from Interface builder selected my segue Its kind was "Present Modally" and its presentation was "Over current context"

我遇到了同样的问题我所做的是从界面构建器中选择了我的 segue 它的类型是“当前模态”,它的表示是“当前上下文”

i changed the presentation to "Default", and then it worked for me.

我将演示文稿更改为“默认”,然后它对我有用。

回答by Chris Herbst

What worked for me was to add the presentation of the alert to the main thread.

对我有用的是将警报的呈现添加到主线程。

DispatchQueue.main.async {
   self.present(alert, animated: true)
}

The presentation of the current viewController was not complete. By adding the alert to the main thread it can wait for the viewController's presentation to complete before attempting to present.

当前 viewController 的呈现不完整。通过将警报添加到主线程,它可以在尝试呈现之前等待 viewController 的呈现完成。

回答by Mehrdad

My problem was that (in my coordinator) i had presented a VCon a VCand then when i wanted to present the next VC(third one), presented the third VC from the first one which obviously makes the problem which is already presenting. make sure you are presenting the third one from the second VC.

我的问题是(在我的协调员中)我在 aVC上展示了一个VC,然后当我想展示下一个 VC(第三个)时,展示了第一个 VC 中的第三个 VC,这显然是问题所在which is already presenting。确保您展示的是第二个中的第三个VC

secondVC.present(thirdVC, animated: true, completion: nil)