如何closures在Swift ViewController?

我试图通过在IBAction调用dismissViewController在swift中closuresViewController

  @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永远不会被解雇。 可能是什么问题呢?

从你的形象看来,你使用推送呈现的ViewController

你需要使用

 navigationController.popViewControllerAnimated(true) 

dismissViewControllerAnimated用于closures使用模态显示的ViewControllers

Swift 3.0更新:

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

我有你的问题的解决scheme。 如果使用模态呈现视图,请尝试使用以下代码来closures视图控制器:

Swift 3:

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

要么

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

 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") } } 
  1. 在NavigationController中embedded您想要closures的视图
  2. 添加一个“完成”作为标识符的BarButton
  3. 调用“辅助编辑器”,并select“完成”button
  4. 为这个button创build一个IBAction
  5. 将这一行添加到括号中:

     self.dismissViewControllerAnimated(true, completion: nil) 

使用:

 self.dismissViewControllerAnimated(true, completion: nil) 

代替:

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

在Swift 3.0中,将其input到函数中一样简单:

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

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

 self.navigationController?.popViewController(animated: true) 

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

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

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

这是解除当前视图控制器并返回到前一个视图控制器的一种方法。 你只能通过Storyboard来做到这一点。

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

请尝试一下,这是与我合作。

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

祝你好运

不要从取消或完成到其他VC创build任何segue ,只写这个代码你的button@IBAction

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

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

然后我调用这个方法来closures任何UIViewController子类中的视图控制器。 例如,在取消操作中:

 class MyViewController: UIViewController { ... @IBAction func cancel(sender: AnyObject) { dismissMe(animated: true, completion: nil) } ... } 

作为参考,请注意,您可能会解散错误的视图控制器。 例如,如果您有一个警告框或模式显示在另一个模式之上。 (例如,您可以在当前的模式警报上显示一个Twitter发布警报)。 在这种情况下,你需要打电话解雇两次,或者使用放松的继续。

如果你正在呈现一个ViewController的模态,并且想返回到根ViewController,注意closures这个模态表示的ViewController,然后再回到根ViewController,否则这个ViewController将不会从内存中移除,并导致内存泄漏。

在Swift 3.0中

如果你想解雇一个呈现的视图控制器

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

这个代码写在button动作来解雇

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