最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]ViewController去presentViewController时出错:Presenting view controllers on detached view controllers is discouraged

iOS crifan 1653浏览 0评论

代码:

func showAlert(vc:UIViewController, title:String, message:String? = nil){

    dispatchMain_async({

        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

        let sureAlertAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Destructive, handler: nil)

        alertController.addAction(sureAlertAction)

        vc.presentViewController(alertController, animated: true, completion: nil)

    })

}

当前vc,已经通过点击导航栏的返回按钮,消失了,然后调用到:

vc.presentViewController(alertController, animated: true, completion: nil)

出错:

Presenting view controllers on detached view controllers is discouraged

界面效果:

此处情况是:

调用

vc.presentViewController(alertController, animated: true, completion: nil)

对应的vc,已经(通过点击导航栏中的返回按钮)消失了

也的确是有问题的。

Presenting view controllers on detached view controllers is discouraged 

ios – Getting warning like Presenting view controllers on detached view controllers is discouraged – Stack Overflow

Swift: "Presenting view controllers on detached view controllers is discouraged"

ios – Presenting view controllers on detached view controllers – Stack Overflow

ios – Presenting view controllers on detached view controllers is discourage – Stack Overflow

swift check view controller not detached

最后改为:

func showAlert(vc:UIViewController, title:String, message:String? = nil){

    dispatchMain_async({

        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

        let sureAlertAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Destructive, handler: nil)

        alertController.addAction(sureAlertAction)

        if isCurrentShowingVc(vc) {

            vc.presentViewController(alertController, animated: true, completion: nil)

        }

    })

}

即可。

至少目前实现了:

当前的vc不是当前页面的话,就不提示了。

-》之前的case是:

点击返回按钮,当前页面已经不在导航栏的视图堆栈中了,所以就不显示了。

转载请注明:在路上 » [已解决]ViewController去presentViewController时出错:Presenting view controllers on detached view controllers is discouraged

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
66 queries in 0.131 seconds, using 18.90MB memory