[英]Dismissing a View Controller and displaying another View Controller
Now on a button click from V2, ViewController should be dismissed back to V1 and display ViewController V3. 现在,从V2上单击按钮,应将ViewController退回到V1并显示ViewController V3。
I tried this piece of code to achieve this functionality. 我尝试了这段代码来实现此功能。 It's not working though.
虽然没有用。 Please help!
请帮忙!
Note : V2 and V3 are in same StoryBoard (Start). 注意 :V2和V3在同一StoryBoard中(开始)。 V1 is in different story board(main).
V1在不同的故事板(主)中。
var VC3 : ViewController3?
let mainStoryboard: UIStoryboard = UIStoryboard(name: "start",bundle: nil)
self.dismissViewControllerAnimated(false, completion: {
self.VC3 = (mainStoryboard.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3)
self.presentViewController(self.VC3!, animated: true, completion: nil)
})
Update - I get this warning Attempt to present ViewController3 on ViewController2 whose view is not in the window hierarchy! 更新 -我收到此警告尝试在视图控制器不在窗口层次结构中的ViewController2上显示ViewController3!
I replicated your query and created 3 View Controllers: VC1, VC2 and VC3. 我复制了您的查询并创建了3个视图控制器:VC1,VC2和VC3。 I achieve this using Delegates and Segues.
我使用Delegates和Segues实现了这一目标。 The Storyboard Setup .
情节提要设置 。
VC1.Swift: VC1.Swift:
class VC1: UIViewController, VC2Delegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func showVC3() {
self.performSegueWithIdentifier("VC1ToVC3", sender: nil)
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "VC1ToVC2" {
if let destVC = segue.destinationViewController as? VC2 {
destVC.delegate = self
}
}
}
}
VC2.Swift: VC2.Swift:
protocol VC2Delegate {
func showVC3()
}
class VC2: UIViewController {
var delegate:VC2Delegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func actBack(){
self.dismissViewControllerAnimated(true, completion: nil)
if let _ = delegate {
delegate?.showVC3()
}
}
}
The warning states that "You are presenting ViewController3 on ViewController2, But ViewController2 is not there in the view hierarchy!". 警告指出“您正在ViewController2上显示ViewController3,但是ViewController2在视图层次结构中不存在!”。 This means ViewController2 has dismissed before ViewController3 would be presented.
这意味着ViewController2在显示ViewController3之前已经关闭。 So it is not possible to present ViewController 3 on ViewController2
因此,不可能在ViewController2上显示ViewController 3
Use this sample code: 使用以下示例代码:
import UIKit
class ViewController: UIViewController, presentViewControllerThree{
@IBAction func displayViewControllerTwoOnClick(_ sender: AnyObject) {
let currentStoryboard: UIStoryboard = UIStoryboard(name: "Start",bundle: nil)
let vc2: ViewController2 = currentStoryboard.instantiateViewController(withIdentifier: "viewcont2") as! ViewController2
vc2.delegate = self
self.navigationController?.present(vc2, animated: true, completion: nil)
}
func presentVC3(){
let currentStoryboard: UIStoryboard = UIStoryboard(name: "Start",bundle: nil)
let vc3: ViewController3 = currentStoryboard.instantiateViewController(withIdentifier: "viewcont3") as! ViewController3
self.navigationController?.present(vc3, animated: true, completion: nil)
}
}
import UIKit
protocol presentViewControllerThree {
func presentVC3()
}
class ViewController2: UIViewController {
var delegate: presentViewControllerThree?
@IBAction func dismissViewControllerOnClick(_ sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
delegate?.presentVC3()
}
}
import UIKit
class ViewController3: UIViewController {
@IBAction func dismissViewControllerOnClick(_ sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
}
}
Please check my GitHub link to test sample project: 请检查我的GitHub链接以测试示例项目:
https://github.com/k-sathireddy/DismissPresentViewControllers https://github.com/k-sathireddy/DismissPresentViewControllers
The error message is quite descriptive: 该错误消息具有很强的描述性:
Attempt to present ViewController3 on ViewController2 whose view is not in the window hierarchy!
尝试在视图控制器不在窗口层次结构中的ViewController2上显示ViewController3!
Lets take a look at your existing code: 让我们看一下您现有的代码:
self.dismissViewControllerAnimated(false, completion: {
self.VC3 = (mainStoryboard.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3)
//Using self here is causing that error
self.presentViewController(self.VC3!, animated: true, completion: nil)
})
In the completion handler, you are trying to present VC3
from self
...but self
refers to the ViewController
you just dismissed. 在完成处理程序中,您尝试从
self
呈现VC3
...,但是self
指的是您刚刚关闭的ViewController
。 Thats what that error message is telling you. 那就是该错误消息告诉您的内容。
Instead, you need to present VC3 from the parent of VC2 . 相反, 您需要从VC2的父级中呈现VC3 。 One way you can achieve that is through the
presentingViewController
property: 实现此目标的一种方法是通过
presentingViewController
属性:
self.dismissViewControllerAnimated(false, completion: {
self.VC3 = (mainStoryboard.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3)
self.presentingViewController.presentViewController(self.VC3!, animated: true, completion: nil)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.