在 Swift 中关闭和显示视图控制器 [英] Dismiss and Present View Controller in Swift

查看:19
本文介绍了在 Swift 中关闭和显示视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试呈现一个视图控制器并关闭我当前的模式视图,但此代码不起作用

Hi I'm trying to present a viewcontroller and dismiss my current modal view but this code is not working

self.dismissViewControllerAnimated(true, completion: {
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
    self.presentViewController(vc!, animated: true, completion: nil)
})

反之亦然对presentviewcontroller的完成块不起作用

vice versa is not working too on completion block of presentviewcontroller

替换了 vc!给自己

推荐答案

您必须获得呈现自我的视图控制器(当前的视图控制器).如果该视图控制器是 rootViewController,那么您可以使用下面的代码,如果不是,则根据您的视图控制器层次结构查询它.

You have to get the viewController which presented self (current ViewController). If that view controller is rootViewController, then you can use the code below, if not then query it based on your view controller hierarchy.

if let vc3 = self.storyboard?.instantiateViewController(withIdentifier: "vc3") as? ViewController3 {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController!.present(vc3, animated: true, completion: nil)
}

这篇关于在 Swift 中关闭和显示视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆