簡體   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