[英]Dismissing a View Controller and displaying another View Controller
現在,從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個視圖控制器:VC1,VC2和VC3。 我使用Delegates和Segues實現了這一目標。 情節提要設置 。
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()
}
}
}
警告指出“您正在ViewController2上顯示ViewController3,但是ViewController2在視圖層次結構中不存在!”。 這意味着ViewController2在顯示ViewController3之前已經關閉。 因此,不可能在ViewController2上顯示ViewController 3
使用以下示例代碼:
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)
}
}
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()
}
}
import UIKit
class ViewController3: UIViewController {
@IBAction func dismissViewControllerOnClick(_ sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
}
}
請檢查我的GitHub鏈接以測試示例項目:
https://github.com/k-sathireddy/DismissPresentViewControllers
該錯誤消息具有很強的描述性:
嘗試在視圖控制器不在窗口層次結構中的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)
})
在完成處理程序中,您嘗試從self
呈現VC3
...,但是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)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.