如何在使用 Swift 呈现新的 Viewcontroller 后关闭之前的 Viewcontroller?
How to dismiss previous Viewcontroller after presenting new Viewcontroller using Swift?
我的场景,在我的项目中,我维护了三个 ViewController
(Main, VC1 and VC2)
。在主要 ViewController
中,我正在维护 UIButton
,此按钮单击以 VC1
呈现模型视图控制器。同样,VC1
我保持 UIButton
并点击操作将模型呈现给 VC2
。 VC2
展示后我需要关闭 VC1。
// presenting ViewController
var presentingViewController: UIViewController! = self.presentingViewController
self.dismissViewControllerAnimated(false) {
// go back to MainMenuView as the eyes of the user
presentingViewController.dismissViewControllerAnimated(false, completion: nil)
}
在 VC2
的关闭按钮操作中试试这个
var vc = self.navigationController?.presentingViewController
while vc?.presentingViewController != nil {
vc = vc?.presentingViewController
}
vc?.dismiss(animated: true, completion: nil)
希望这会奏效:
您需要有一个 UIViewController 作为基础,在本例中 MainViewController
是基础 ViewController。需要使用协议调用Controller之间的导航
你可以使用协议:-
进入你的第一个ViewController设置协议:
protocol FirstViewControllerProtocol {
func dismissViewController()
}
class FirstViewController: UIViewController {
var delegate:FirstViewControllerProtocol!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func goBack(sender: AnyObject) {
self.dismissViewControllerAnimated(true) {
self.delegate!.dismissViewController()
}
}
现在在您的 MainViewController
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 for dismiss
func dismissViewController() {
if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(SecondViewController)){
self.presentViewController(viewController, animated: true, completion: nil)
}
}
在这种情况下,您需要从显示其他视图控制器的视图控制器(在您的情况下为 Main)调用 dismiss。
正如您在上述问题中所述,Main 呈现 VC1,然后 VC1 呈现 VC2:
然后调用 Main.dismiss(animated: true, completion: nil) 将同时关闭 VC1 和 VC2。
如果您没有对根控制器 (Main) 的引用,您可以链接几个 presentingViewController 属性来访问它;在最顶层的控制器(VC2)中是这样的:
presentingViewController?.presentingViewController?.dismiss(animated: true)
希望对您有所帮助。
有一个 present(_:animated:completion:) 方法需要完成。在里面你可以 dismiss
你的 VC1.
您的代码将如下所示
@IBAction func ButtonTapped(_ sender: Any) {
present(VC2, animated: true) {
dismiss(animated: true, completion: nil)
}
}
要解决问题陈述,您可以做的是 VC2
使用 Main
而不是 VC1
。
我们可以使用
在 VC1
中获取对 Main
的引用
self.presentingViewController
当VC2
出现时,在present(_:animated:completion:)
方法的completionHandler
中关闭VC1
class Main: UIViewController {
@IBAction func onTapButton(_ sender: UIButton) {
let vc1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "VC1")
self.present(vc1, animated: true, completion: nil)
}
}
class VC1: UIViewController {
@IBAction func onTapButton(_ sender: UIButton) {
let vc2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "VC2")
vc2.view.backgroundColor = .blue
self.dismiss(animated: false, completion: nil)
self.presentingViewController?.present(vc2, animated: true, completion: nil)
}
}
class VC2: UIViewController {
}
这种方法给出了预期的输出。让我以防万一还需要什么。
您需要从 mainVC 打开 vc2 - 为此,您需要一个委托方法,它会告诉 mainVC 关闭当前打开的 vc(即 vc 1) 并在成功块中打开 vc2.
代码片段:-
解雇(动画:假){
//打开vc2
目前(vc2)
}
我的场景,在我的项目中,我维护了三个 ViewController
(Main, VC1 and VC2)
。在主要 ViewController
中,我正在维护 UIButton
,此按钮单击以 VC1
呈现模型视图控制器。同样,VC1
我保持 UIButton
并点击操作将模型呈现给 VC2
。 VC2
展示后我需要关闭 VC1。
// presenting ViewController
var presentingViewController: UIViewController! = self.presentingViewController
self.dismissViewControllerAnimated(false) {
// go back to MainMenuView as the eyes of the user
presentingViewController.dismissViewControllerAnimated(false, completion: nil)
}
在 VC2
的关闭按钮操作中试试这个
var vc = self.navigationController?.presentingViewController
while vc?.presentingViewController != nil {
vc = vc?.presentingViewController
}
vc?.dismiss(animated: true, completion: nil)
希望这会奏效:
您需要有一个 UIViewController 作为基础,在本例中 MainViewController
是基础 ViewController。需要使用协议调用Controller之间的导航
你可以使用协议:-
进入你的第一个ViewController设置协议:
protocol FirstViewControllerProtocol {
func dismissViewController()
}
class FirstViewController: UIViewController {
var delegate:FirstViewControllerProtocol!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func goBack(sender: AnyObject) {
self.dismissViewControllerAnimated(true) {
self.delegate!.dismissViewController()
}
}
现在在您的 MainViewController
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 for dismiss
func dismissViewController() {
if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(SecondViewController)){
self.presentViewController(viewController, animated: true, completion: nil)
}
}
在这种情况下,您需要从显示其他视图控制器的视图控制器(在您的情况下为 Main)调用 dismiss。
正如您在上述问题中所述,Main 呈现 VC1,然后 VC1 呈现 VC2: 然后调用 Main.dismiss(animated: true, completion: nil) 将同时关闭 VC1 和 VC2。
如果您没有对根控制器 (Main) 的引用,您可以链接几个 presentingViewController 属性来访问它;在最顶层的控制器(VC2)中是这样的:
presentingViewController?.presentingViewController?.dismiss(animated: true)
希望对您有所帮助。
有一个 present(_:animated:completion:) 方法需要完成。在里面你可以 dismiss
你的 VC1.
您的代码将如下所示
@IBAction func ButtonTapped(_ sender: Any) {
present(VC2, animated: true) {
dismiss(animated: true, completion: nil)
}
}
要解决问题陈述,您可以做的是 VC2
使用 Main
而不是 VC1
。
我们可以使用
在VC1
中获取对 Main
的引用
self.presentingViewController
当VC2
出现时,在present(_:animated:completion:)
方法的completionHandler
中关闭VC1
class Main: UIViewController {
@IBAction func onTapButton(_ sender: UIButton) {
let vc1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "VC1")
self.present(vc1, animated: true, completion: nil)
}
}
class VC1: UIViewController {
@IBAction func onTapButton(_ sender: UIButton) {
let vc2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "VC2")
vc2.view.backgroundColor = .blue
self.dismiss(animated: false, completion: nil)
self.presentingViewController?.present(vc2, animated: true, completion: nil)
}
}
class VC2: UIViewController {
}
这种方法给出了预期的输出。让我以防万一还需要什么。
您需要从 mainVC 打开 vc2 - 为此,您需要一个委托方法,它会告诉 mainVC 关闭当前打开的 vc(即 vc 1) 并在成功块中打开 vc2.
代码片段:-
解雇(动画:假){ //打开vc2 目前(vc2) }