如何将(可选的)完成处理程序闭包传递给Swift中的transitionFromViewController?

时间:2014-10-11 13:29:52

标签: ios uiviewcontroller swift

在我的应用程序的ViewController中,我调用transitionFromViewController,但在将一个闭包传递给completion:参数时总是会出现以下错误。

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)?)

2 个答案:

答案 0 :(得分:2)

您无法将nil传递给声明为animations的{​​{1}}参数() -> Void()

如果需要,请传递空optional

closure

答案 1 :(得分:2)

我认为以下代码可以帮助您:

self.transitionFromViewController(fromViewController, toViewController: toViewController, duration: 0.1, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            // code for animations
            }) { (value: Bool) -> Void in
            // code after completion
        }

其中,//动画代码 - 您想在块中执行的代码。 并且,//完成后的代码 - 在块完成后要执行的代码。