如何在Swift中将(可选)完成处理函数闭包传递给transitionFromViewController?
问题描述:
在我的应用程序的ViewController中,我调用了transitionFromViewController,但在将闭包传递给completion:参数时总是得到以下错误。如何在Swift中将(可选)完成处理函数闭包传递给transitionFromViewController?
Type '() -> Void' does not conform to protocol 'NilLiteralConvertible'
这里的函数调用:
self.transitionFromViewController(
self.currentVC,
toViewController: newController,
duration: 0.2,
options: UIViewAnimationOptions.TransitionCrossDissolve,
nil,
completion: { finished in
fromViewController.removeFromParentViewController()
toViewController.didMoveToParentViewController(containerViewController)
toViewController.view.frame = containerViewController.view.bounds
})
根据代码完成方法签名如下:
transitionFromViewController(fromViewController: UIViewController, toViewController: UIViewController, duration: NSTimeInterval, options: UIViewAnimationOptions, animations:() -> Void(), completion: ((Bool) -> Void)?)
答
你不能传递零至animations
宣布为不optional
() -> Void()
传递空closure
如果你想
self.transitionFromViewController(
self.currentVC,
toViewController: newController,
duration: 0.2,
options: UIViewAnimationOptions.TransitionCrossDissolve,
animations: {() -> Void in
},
completion: { finished in
fromViewController.removeFromParentViewController()
toViewController.didMoveToParentViewController(containerViewController)
toViewController.view.frame = containerViewController.view.bounds
})
答
我想下面的代码应该帮助你:哪里,//用于动画代码
self.transitionFromViewController(fromViewController, toViewController: toViewController, duration: 0.1, options: UIViewAnimationOptions.CurveEaseInOut, animations: {() -> Void in
// code for animations
}) { (value: Bool) -> Void in
// code after completion
}
- 代码你想要的块期间执行。 而且,//完成之后的代码 - 您要在块完成之后执行的代码。
在删除之前,应根据文档调用willMoveToParentViewController(nil)! – 2015-11-16 15:02:38