关闭View Controller并显示另一个View Controller

时间:2016-10-03 02:45:44

标签: ios swift uiviewcontroller

  • 我有三个ViewControllers - V1 V2和V3。
  • 点击V1按钮,ViewController V2显示动画 FlipHorizo​​ntal。
  • 现在点击V2的按钮,ViewController应该被解雇 返回V1并显示ViewController V3。

    我尝试使用这段代码来实现此功能。它不起作用。请帮忙!

注意:V2和V3在同一个StoryBoard(开始)中。 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)
        })

更新 - 我收到此警告 尝试在ViewController2上呈现ViewController3,其视图不在窗口层次结构中!

3 个答案:

答案 0 :(得分:3)

我复制了您的查询并创建了3个视图控制器:VC1,VC2和VC3。 我使用Delegates和Segues实现了这一点。 Storyboard Setup

<强> 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:

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()
        }
    }

}

答案 1 :(得分:1)

警告声明“您正在ViewController2上呈现ViewController3,但ViewController2在视图层次结构中不存在!”。这意味着ViewController2在呈现ViewController3之前已被解雇。因此无法在ViewController2上显示ViewController 3

使用此示例代码:

ViewController1

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)
    }

}

ViewController2

 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()

    }

}

ViewController3

   import UIKit

class ViewController3: UIViewController {

    @IBAction func dismissViewControllerOnClick(_ sender: AnyObject) {

        self.dismiss(animated: true, completion: nil)
    }


}

Main.storyboard屏幕截图:

enter image description here

Start.storyboard:

enter image description here

请检查我的GitHub链接以测试示例项目:

https://github.com/k-sathireddy/DismissPresentViewControllers

答案 2 :(得分:0)

错误消息非常具有描述性:

  

尝试在视图为的ViewController2上显示ViewController3   不在窗口层次结构中!

让我们看看你现有的代码:

 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)
})

在完成处理程序中,您尝试显示VC3中的self ...但self指的是您刚解雇的ViewController。那是错误信息告诉你的。

相反,您需要从VC2的父级提供VC3 。实现这一目标的一种方法是通过presentingViewController属性:

 self.dismissViewControllerAnimated(false, completion: {
            self.VC3 = (mainStoryboard.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3)
            self.presentingViewController.presentViewController(self.VC3!, animated: true, completion: nil)
})