在使用Swift介绍新的ViewController之后,如何忽略以前的ViewController



我的场景,在我的项目中,我维护了三个ViewController (Main, VC1 and VC2)。在主ViewController中,我正在维护UIButton,此按钮单击VC1呈现模型视图控制器。同样,VC1我使用动作单击将UIButton维护为VC2VC2提出后,我需要解散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的关闭按钮Action

中尝试一下
var vc = self.navigationController?.presentingViewController
while vc?.presentingViewController != nil {
    vc = vc?.presentingViewController
}
vc?.dismiss(animated: true, completion: nil)

希望这能起作用:

您需要将UiviewController作为基础,在这种情况下,MainViewController是基本ViewController。您需要使用协议调用控制器之间的导航。

您可以使用协议进行: -

进入您的FirstViewController设置协议:

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而不是VC1来呈现VC2

我们可以使用

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(即VC1(和成功块打开VC2。

代码狙击: -

解雇(动画:false({ //打开VC2 现在(VC2(}

在这种情况下,您需要从呈现其他视图控制器的视图控制器中调用解雇(在您的情况下为主要(。

正如您在上面的问题中所述的情况,主介绍VC1和VC1,然后呈现VC2:然后致电main.dismiss(动画:true,完成:nil(将同时解雇VC1和VC2。

如果您没有对根控制器的引用(main(,则可以链接几个呈现视图controller属性来访问它;类似的东西在最高控制器(VC2(中:

presentingViewController?.presentingViewController?.dismiss(animated: true)

我希望这会有所帮助。

有一个礼物(_:animated:interion:postance :)方法可以完成。在其中您可以dismiss您的VC1。

您的代码看起来像这样

@IBAction func ButtonTapped(_ sender: Any) {
    present(VC2, animated: true) {
        dismiss(animated: true, completion: nil)
    }
}

最新更新