如何在Swift中解除ViewController?

问题描述 投票:185回答:17

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

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

ios swift viewcontroller
17个回答
371
投票

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

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

斯威夫特2

navigationController.popViewControllerAnimated(true)

斯威夫特4

navigationController?.popViewController(animated: true)

dismiss(animated: true, completion: nil)

4
投票

不要从Cancel或Done创建任何segue到其他VC,只需将此代码写入您的按钮@IBAction

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

3
投票

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

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

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

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

好运..


2
投票

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


1
投票

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


1
投票

在Swift 3.0中

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

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

1
投票

在Swift 4.1和Xcode 9.4.1中

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

self.navigationController?.popViewController(animated: false)

0
投票

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

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

0
投票

试试这个:

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

161
投票

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

斯威夫特3:

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

要么

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

self.navigationController?.popViewController(animated: true)

19
投票

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

13
投票
  1. 在NavigationController中嵌入要关闭的视图
  2. 添加一个带有“Done”作为标识符的BarButton
  3. 选中“完成”按钮调用“助理编辑器”
  4. 为此按钮创建一个IBAction
  5. 将此行添加到括号中: self.dismissViewControllerAnimated(true, completion: nil)

12
投票

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

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

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

self.navigationController?.popViewController(animated: true)

11
投票

使用:

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

代替:

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

7
投票

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

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

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


6
投票

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

5
投票

来自Apple documentations

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

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

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

presentingViewController?.dismiss(animated: true, completion: nil)
© www.soinside.com 2019 - 2024. All rights reserved.