簡體   English   中英

如何在 Swift 中關閉 ViewController?

[英]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)
  1. 將要關閉的視圖嵌入到 NavigationController 中
  2. 添加一個帶有“完成”作為標識符的 BarButton
  3. 在選中“完成”按鈕的情況下調用“助理編輯器”
  4. 為這個按鈕創建一個 IBAction
  5. 將此行添加到括號中:

     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 將不為零並且代碼將被執行。

來自 Apple 文檔

呈現視圖控制器負責解除它呈現的視圖控制器

因此,僅從自身調用dismiss方法是一種不好的做法。

如果您以模態呈現它,您應該做的是:

presentingViewController?.dismiss(animated: true, completion: nil)

根據我的經驗,我添加了一個方法來將我解雇為 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 執行此操作。

  1. 打開故事板
  2. 右鍵單擊“取消”按鈕並將其拖動到上一個視圖控制器,您要在此處移回上一個控制器
  3. 現在釋放右鍵單擊,您可以看到在取消按鈕上執行的一些操作
  4. 現在從列表中選擇“popover present”選項
  5. 現在您可以通過單擊取消按鈕關閉當前視圖

請試試這個,它正在與我合作。

第二種方式 - 使用 - 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM