[英]How to dismiss ViewController in Swift?
我試圖通過在IBAction
調用dismissViewController
來迅速解除一個ViewController
@IBAction func cancel(sender: AnyObject) {
self.dismissViewControllerAnimated(false, completion: nil)
println("cancel")
}
@IBAction func done(sender: AnyObject) {
self.dismissViewControllerAnimated(false, completion: nil)
println("done")
}
我可以在控制台輸出中看到 println 消息,但 ViewController 永遠不會被解雇。 可能是什么問題呢?
從您的圖像看來,您似乎使用 push 呈現了 ViewController
dismissViewControllerAnimated
用於關閉使用 modal 呈現的 ViewControllers
斯威夫特 2
navigationController.popViewControllerAnimated(true)
斯威夫特 4
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
我有你的問題的解決方案。 如果您使用模態顯示視圖,請嘗試使用此代碼關閉視圖控制器:
斯威夫特 3:
self.dismiss(animated: true, completion: nil)
或者
如果您使用“推”轉場呈現視圖
self.navigationController?.popViewController(animated: true)
如果您這樣做,我想您可能不會在控制台中收到 println 消息,
@IBAction func cancel(sender: AnyObject) {
if(self.presentingViewController){
self.dismissViewControllerAnimated(false, completion: nil)
println("cancel")
}
}
@IBAction func done(sender: AnyObject) {
if(self.presentingViewController){
self.dismissViewControllerAnimated(false, completion: nil)
println("done")
}
}
在 Swift 3.0 到 4.0 中,只需在函數中輸入以下內容即可:
self.dismiss(animated: true, completion: nil)
或者,如果您在導航控制器中,您可以“彈出”它:
self.navigationController?.popViewController(animated: true)
將此行添加到括號中:
self.dismissViewControllerAnimated(true, completion: nil)
用:
self.dismiss(animated: true, completion: nil)
代替:
self.navigationController.dismissViewControllerAnimated(true, completion: nil)
如果您呈現一個沒有導航控制器的控制器,您可以從呈現的控制器的方法中調用以下代碼。
self.presentingViewController?.dismiss(animated: true, completion: nil)
如果您的 ViewController 以模態呈現,則可選的presentingViewController 將不為零並且代碼將被執行。
根據我的經驗,我添加了一個方法來將我解雇為 UIViewController 的擴展:
extension UIViewController {
func dismissMe(animated: Bool, completion: (()->())?) {
var count = 0
if let c = self.navigationController?.viewControllers.count {
count = c
}
if count > 1 {
self.navigationController?.popViewController(animated: animated)
if let handler = completion {
handler()
}
} else {
dismiss(animated: animated, completion: completion)
}
}
}
然后我調用這個方法來關閉任何UIViewController
子類中的視圖控制器。 例如,在取消操作中:
class MyViewController: UIViewController {
...
@IBAction func cancel(sender: AnyObject) {
dismissMe(animated: true, completion: nil)
}
...
}
不要創建從 Cancel 或 Done 到其他 VC 的任何 segue並且只編寫此代碼您的按鈕 @IBAction
@IBAction func cancel(sender: AnyObject) {
dismiss(animated: false, completion: nil)
}
這是關閉當前視圖控制器並返回到前一個視圖控制器的一種方法。 您只能通過 Storyboard 執行此操作。
請試試這個,它正在與我合作。
第二種方式 - 使用 - navigationController.popViewControllerAnimated(true)
好運..
由於您使用了推送呈現的視圖控制器,因此,您可以使用
self.dismiss(animated: false, completion: nil)
作為參考,請注意您可能會忽略錯誤的視圖控制器。 例如,如果您有一個警告框或模態顯示在另一個模態之上。 (例如,您可以在當前的模式警報之上顯示 Twitter 帖子警報)。 在這種情況下,您需要調用兩次dismiss,或者使用unwind segue。
所以如果你想關閉你的 Viewcontroller 使用這個。 此代碼是在按鈕操作中編寫以關閉 VC
@IBAction func cancel(sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
嘗試這個:
@IBAction func close() {
dismiss(animated: true, completion: nil)
}
如果您在父 VC 中使用當前方法,則應調用此函數,以解除子 VC 使用此方法
self.dismiss(animated: true, completion: nil)
如果您使用 push 方法調用子 VC,要關閉子 VC,請使用此方法
self.navigationController?.popViewController(animated: true)
如果您以模態方式呈現一個 ViewController,並且想返回到根 ViewController,請在返回根 ViewController 之前注意關閉這個以模態方式呈現的 ViewController,否則該 ViewController 將不會從內存中刪除並導致內存泄漏。
在 Swift 3.0 中
如果你想關閉一個呈現的視圖控制器
self.dismiss(animated: true, completion: nil)
在 Swift 4.1 和 Xcode 9.4.1 中
如果您使用 pushViewController 來呈現新的視圖控制器,請使用此
self.navigationController?.popViewController(animated: false)
@IBAction func back(_ sender: Any) {
self.dismiss(animated: false, completion: nil)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.