![](/img/trans.png)
[英]Present a view controller, dismiss it and present a different one in Swift
[英]Dismiss and Present View Controller in Swift
嗨,我正在尝试呈现一个视图控制器并关闭我当前的模式视图,但此代码不起作用
self.dismissViewControllerAnimated(true, completion: {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
self.presentViewController(vc!, animated: true, completion: nil)
})
反之亦然在presentviewcontroller的完成块上不起作用
编辑:替换vc! 对自己
您必须获得呈现自我的视图控制器(当前的视图控制器)。 如果该视图控制器是 rootViewController,那么您可以使用下面的代码,如果不是,则根据您的视图控制器层次结构查询它。
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)
}
你不能这样做,因为当 UIViewController A 调用 UIViewController B 并且第一个控制器被解除时,两个控制器为零。
您需要有一个 UIViewController 作为基础,在这种情况下 MainViewController 是基础。 您需要使用协议来调用控制器之间的导航。
您可以使用协议来做,例如如下:-
在您的 viewController 设置协议中:
protocol FirstViewControllerProtocol {
func dismissViewController()
}
class FirstViewController: UIViewController {
var delegate:FirstViewControllerProtocol!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func goBack(sender: AnyObject) {
self.dismissViewControllerAnimated(true) {
self.delegate!.dismissViewController()
}
}
现在在您的主视图控制器中
class MainViewController: UIViewController, FirstViewControllerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func goToFirstViewController(sender: AnyObject) {
let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(FirstViewController)) as! FirstViewController
viewController.delegate = self
self.presentViewController(viewController, animated: true, completion: nil)
}
//MARK: Protocol
func dismissViewController() {
if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(SecondViewController)){
self.presentViewController(viewController, animated: true, completion: nil)
}
}
我认为您的代码中有一个错误,其中“self”应该是呈现“vc”的呈现视图控制器,而不是“vc”自身
你的代码
self.dismissViewControllerAnimated(true, completion: {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
vc!.presentViewController(vc!, animated: true, completion: nil)
})
试试这个
self.dismissViewControllerAnimated(true, completion: {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
self.presentViewController(vc!, animated: true, completion: nil)
})
希望这是有帮助的
let parent = self.parentViewController!
parent.dismissViewControllerAnimated(true, completion: {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
parent.presentViewController(vc!, animated: true, completion: nil)
})
这是 Swift3 的解决方案
呈现 ViewController
let NotificationVC = self.storyboard?.instantiateViewController(withIdentifier: "NotificationVC") as! ExecutiveNotificationViewController
self.present(NotificationVC, animated: true, completion: nil)
要关闭 ViewController:
self.dismiss(animated: true, completion: nil)
斯威夫特4&5
weak var presentingVC = self.presentingViewController
self.dismiss(animated: true, completion: {
let vc = SecondController()
presentingVC?.present(vc, animated: true, completion: nil)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.