最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

iOS crifan 4245浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.153 seconds, using 22.16MB memory