繁体   English   中英

在使用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)

为了解决问题陈述,您可以做的是使用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。

代码片段:-

dismiss(动画:false){//打开vc2 present(vc2)}

希望这会起作用:

您需要有一个UIViewController作为基础,在这种情况下, MainViewController是基础ViewController。 您需要使用协议来调用控制器之间的导航。

您可以使用协议:

在您的FirstViewController设置Protocol中:

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,complete: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)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM