[英]Swift dismiss current Viewcontroller after presenting new 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.