【问题标题】:How to dismiss ViewController in Swift?如何在 Swift 中关闭 ViewController?
【发布时间】:2014-08-31 08:56:20
【问题描述】:

我正在尝试通过在 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 永远不会被解雇。可能是什么问题呢?

【问题讨论】:

  • 你是如何展示视图控制器的?
  • 我通过设置一个 segue 进行映射 - “显示”,请参见随附的屏幕截图。
  • 尝试使用模态。如果你使用 push,你应该使用导航控制器的 pop 方法将其关闭。

标签: ios swift xcode storyboard viewcontroller


【解决方案1】:

从您的图片看来,您似乎使用 push 呈现了 ViewController

dismissViewControllerAnimated 用于关闭使用 modal 呈现的 ViewControllers

斯威夫特 2

navigationController.popViewControllerAnimated(true)

斯威夫特 4

navigationController?.popViewController(animated: true)

dismiss(animated: true, completion: nil)

【讨论】:

  • “Show Detail”转场你会怎么做?
  • 对于 Swift 2.2 navigationController!.popViewControllerAnimated(true)
  • 对于 Swift 3 navigationController!.popViewController(animated: true)
【解决方案2】:

我有一个解决您的问题的方法。如果您使用模态呈现视图,请尝试使用此代码关闭视图控制器:

斯威夫特 3:

self.dismiss(animated: true, completion: nil)

如果您使用“推”segue 呈现视图

self.navigationController?.popViewController(animated: true)

【讨论】:

  • 谢谢,但结果还是一样,它不会关闭 ViewController
  • 这与 OP 使用的方法有何不同?
  • 如果你不回答我的问题,对话是没有用的。
  • _ = self.navigationController?.popViewController(animated: true)
【解决方案3】:

如果你这样做,我猜你可能不会在控制台中收到 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")
  }    
}

【讨论】:

【解决方案4】:

在 Swift 3.0 到 4.0 中,只需在函数中输入以下内容即可:

self.dismiss(animated: true, completion: nil)

或者,如果您在导航控制器中,您可以“弹出”它:

self.navigationController?.popViewController(animated: true)

【讨论】:

  • dismiss 不起作用,因为我正在使用 pushViewController。只有 self.navigationController?.popViewController(animated: true) 工作。
【解决方案5】:
  1. 将要关闭的视图嵌入到 NavigationController 中
  2. 添加一个以“完成”为标识符的 BarButton
  3. 在选中“完成”按钮的情况下调用助理编辑器
  4. 为此按钮创建一个 IBAction
  5. 将此行添加到括号中:

    self.dismissViewControllerAnimated(true, completion: nil)
    

【讨论】:

    【解决方案6】:

    用途:

    self.dismiss(animated: true, completion: nil)
    

    代替:

    self.navigationController.dismissViewControllerAnimated(true, completion: nil)
    

    【讨论】:

    • 添加一个!到导航控制器,它对我有用
    • @naturalc:请注意,如果 navigationController 为 nil 并且您输入 !,应用程序将崩溃
    【解决方案7】:

    如果您呈现一个没有导航控制器的控制器,您可以从呈现的控制器的方法中调用以下代码。

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

    如果你的 ViewController 是模态显示的,可选的 presentingViewController 将不是 nil 并且代码将被执行。

    【讨论】:

      【解决方案8】:

      来自苹果documentations

      呈现视图控制器负责关闭它呈现的视图控制器

      因此,仅从其自身调用dismiss 方法是一种不好的做法。

      如果您以模态形式呈现它,您应该做的是:

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

      【讨论】:

        【解决方案9】:

        根据我的经验,我添加了一个方法来解除我作为 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)
           }
           ...
        }
        

        【讨论】:

          【解决方案10】:

          不要创建任何从 Cancel 或 Done 到其他 VC 的 segue,并且只在按钮 @IBAction 中编写此代码

          @IBAction func cancel(sender: AnyObject) {
              dismiss(animated: false, completion: nil)
          }
          

          【讨论】:

            【解决方案11】:

            这是关闭当前视图控制器并返回到前一个视图控制器的一种方法。您只能通过 Storyboard 执行此操作。

            1. 打开故事板
            2. 右键单击“取消”按钮并将其拖动到上一个视图控制器,您要在此处移回上一个控制器
            3. 现在松开右键,您可以看到取消按钮上执行的一些操作
            4. 现在从列表中选择“popover present”选项
            5. 现在您可以通过单击取消按钮来关闭当前视图

            请试试这个,它对我有用。

            第二种方式 - 使用 - navigationController.popViewControllerAnimated(true)

            祝你好运..

            【讨论】:

            • stackoverflow.com/questions/30840235/…。请问有什么帮助吗?我被困了这么久谁仍然找不到解决方案
            • 这是错误的。您永远不会关闭视图,而是在当前视图控制器之上呈现一个新的视图控制器,从而导致内存泄漏。这很可能会导致您的应用被应用商店拒绝。
            【解决方案12】:

            由于您使用了推送呈现的viewController,因此,您可以使用

            self.dismiss(animated: false, completion: nil)
            

            【讨论】:

              【解决方案13】:

              作为参考,请注意您可能关闭了错误的视图控制器。例如,如果您有一个警报框或模态显示在另一个模态之上。 (例如,您可以在当前模式警报之上显示 Twitter 帖子警报)。在这种情况下,你需要调用两次dismiss,或者使用unwind segue。

              【讨论】:

                【解决方案14】:

                试试这个:

                @IBAction func close() {
                  dismiss(animated: true, completion: nil)
                }
                

                【讨论】:

                • 名为“dismissViewController”的方法只需要一个参数,我想这看起来像是将动画关闭到上一个视图,这是最简单的解决方案。
                【解决方案15】:

                如果您在父 VC 中使用 present 方法,那么您应该调用此函数,以关闭子 VC 使用此

                self.dismiss(animated: true, completion: nil)
                

                如果您使用 push 方法调用子 VC,则使用此方法关闭子 VC

                self.navigationController?.popViewController(animated: true)
                

                【讨论】:

                  【解决方案16】:

                  所以如果你想关闭你的 Viewcontroller 使用这个。这段代码写在按钮动作中以关闭 VC

                    @IBAction func cancel(sender: AnyObject) {
                     dismiss(animated: true, completion: nil)
                    }
                  

                  【讨论】:

                  • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
                  【解决方案17】:

                  如果您以模态方式呈现 ViewController,并且想要返回根 ViewController,请注意在返回根 ViewController 之前关闭此模态呈现的 ViewController,否则此 ViewController 不会从内存中删除并导致内存泄漏.

                  【讨论】:

                    【解决方案18】:

                    在 Swift 3.0 中

                    如果你想关闭呈现的视图控制器

                    self.dismiss(animated: true, completion: nil)
                    

                    【讨论】:

                      【解决方案19】:

                      在 Swift 4.1 和 Xcode 9.4.1 中

                      如果你使用 pushViewController 来展示新的视图控制器,使用这个

                      self.navigationController?.popViewController(animated: false)
                      

                      【讨论】:

                      • 另一个复制粘贴答案
                      【解决方案20】:
                      @IBAction func back(_ sender: Any) {
                              self.dismiss(animated: false, completion: nil)
                          }
                      

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2016-12-19
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2019-11-14
                        • 1970-01-01
                        • 2019-11-15
                        • 1970-01-01
                        相关资源
                        最近更新 更多