首页 文章

如何在Swift中解除ViewController?

提问于
浏览
163

我试图通过在 IBAction 中调用 dismissViewController 来解除swift中的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")
}

random image of a segue

我可以在控制台输出中看到println消息,但ViewController永远不会被解雇 . 可能是什么问题呢?

16 回答

  • 10

    作为参考,请注意您可能正在解雇错误的视图控制器 . 例如,如果您有另一个模态顶部的警告框或模态 . (例如,您可以在当前模态警报的基础上显示Twitter帖子提醒) . 在这种情况下,您需要调用两次dismiss,或使用unwind segue .

  • 4

    如果您以模态方式呈现ViewController,并希望返回到根ViewController,请在返回根ViewController之前关闭此模态呈现的ViewController,否则此ViewController将不会从内存中移除并导致内存泄漏 .

  • 1

    这段代码写在按钮动作中解散

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

    In Swift 3.0

    如果要关闭显示的视图控制器

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

    在Swift 4.1和Xcode 9.4.1中

    如果使用pushViewController呈现新的视图控制器,请使用此方法

    self.navigationController?.popViewController(animated: false)
    
  • 5

    来自Apple documentations

    呈现视图控制器负责解除它所呈现的视图控制器

    因此,从它自己调用dismiss方法是一种不好的做法 .

    如果你提出模态,你应该做的是:

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

    从你的图像看起来你似乎使用push呈现了ViewController

    你需要使用

    navigationController.popViewControllerAnimated(true)
    

    dismissViewControllerAnimated 用于关闭使用模态显示的ViewControllers

    Swift 3.0 Update:

    navigationController?.popViewController(animated: true)
    
    dismiss(animated: true, completion: nil)
    
  • 17

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

    斯威夫特3:

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

    要么

    如果您使用“push”segue显示视图

    self.navigationController?.popViewController(animated: true)
    
  • 10

    如果你这样做我猜你可能不会在控制台中收到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")
      }    
    }
    
  • 3
    • 在NavigationController中嵌入要关闭的视图

    • 添加一个带有"Done"作为标识符的BarButton

    • 选中“完成”按钮调用“助理编辑器”

    • 为此按钮创建IBAction

    • 将此行添加到括号中:

    self.dismissViewControllerAnimated(true, completion: nil)
    
  • 155

    使用:

    self.dismissViewControllerAnimated(true, completion: nil)
    

    代替:

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

    在Swift 3.0到4.0中,就像在函数中键入它一样简单:

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

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

    self.navigationController?.popViewController(animated: true)
    
  • 2

    如果您在没有导航控制器的情况下展示控制器,则可以从所提供控制器的方法调用以下代码 .

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

    如果您的ViewController是以模态方式呈现的,则可选的presentsViewController将不是nil,代码将被执行 .

  • 1

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

    Don't create any segue from Cancel or Done to other VC 并且只在你的按钮@IBAction上写下这段代码

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

    这是解除当前视图控制器并移回到前一个视图控制器的一种方法 . 您只能通过Storyboard执行此操作 .

    • 打开故事板

    • 右键单击“取消”按钮并将其拖动到上一个视图控制器,您可以在其中移回上一个控制器

    • 现在释放右键单击,您可以看到一些在取消按钮上执行的操作

    • 现在从列表中选择"popover present"选项

    • 现在,您可以通过单击取消按钮来关闭当前视图

    请试试这个,它正在和我合作 .

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

    好运..

相关问题