[英]how to show viewcontroller: UIImagePickController and dismiss it (Swift)
[英]Show ViewController and dismiss first
我有一個視圖控制器“ A”,以模態形式顯示了另一個視圖控制器B。
在情節提要中,它看起來像這樣:
UIViewController "A" -> UINavigationViewController "B" -> rootViewController" "C"
我的目標是展示B並隱藏A,我該如何實現?
我嘗試在C中設置屬性,然后解雇了A,但是它不起作用,因為我解雇了C並離開了A。
AViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSomething" {
if let destination = segue.destination as? UINavigationController {
if let viewController = destination.viewControllers.first as? CViewController {
viewController.delegate = self
}
}
}
}
CViewController
override func viewDidLoad() {
super.viewDidLoad()
self.delegate.dismiss(animated: false)
}
取消顯示的View Controller也將自動關閉其層次結構中的顯示的View Controller。
為了更加清楚,讓我們考慮一下,您有三個ViewController: A
, B
和C
,其中A
是根ViewController,它們看起來像:
A => B => C
假設當前顯示的ViewController是B
,-如果我理解正確的話-您想同時顯示C
並關閉B
,正如我提到的,關閉ViewController(在此示例中為B
)會導致關閉其中的所有ViewController層次結構(在此示例中為C
)。
如果要執行以下行為,可能這不是您想要實現的:
從導航A
到B
, B
到C
,當解雇C
,我想去A
,沒有觀察B
。
您需要讓C
ViewController通過執行以下操作來做到這一點:
presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
這導致解散當前呈現的C
ViewController的呈現者(即B
),並直接返回A
有關此的更多信息,您可能需要檢查此答案 。
希望這會有所幫助。
您不能解雇A,因為它正在呈現C。您必須先解雇A,然后使用呈現A的視圖控制器來呈現C。如下所示:
// call this from A
dismiss(animated: false) {
// completion closure
self.presentingViewController.present(cViewController, animated: true)
}
從我的電話回答,所以可能需要一些修復才能編譯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.