简体   繁体   English

UIView.animateWithDuration完成

[英]UIView.animateWithDuration completion

I have a question concerning the swift implementation of the method mentioned in the title. 关于标题中提到的方法的快速实现,我有一个问题。 If I do this: 如果我这样做:

leadingSpaceConstraint.constant = 0
UIView.animateWithDuration(0.3, animations: {
    self.view.layoutIfNeeded()
}, completion: { (complete: Bool) in
    self.navigationController.returnToRootViewController(true)
})

I get the following problem: Missing argument for parameter 'delay' in call. 我遇到以下问题:在调用中缺少参数'delay'的参数。 This only happens if I have the self.navigationController.returnToRootViewController() in the completion part. 只有在完成部分中有self.navigationController.returnToRootViewController()时才会发生这种情况。 If I extract that statement into a seperate method like this: 如果我将该语句提取为一个单独的方法,如下所示:

leadingSpaceConstraint.constant = 0
UIView.animateWithDuration(0.3, animations: {
    self.view.layoutIfNeeded()
}, completion: { (complete: Bool) in
    self.returnToRootViewController()
})

func returnToRootViewController() {
    navigationController.popToRootViewControllerAnimated(true)
}

Then it works perfectly and does exactly what I want. 然后它完美地工作,完全符合我的要求。 Of course this does not seem to be the ideal solution and more like a work around. 当然,这似乎不是理想的解决方案,更像是一种解决方案。 Can anyone tell me what I did wrong or why Xcode (beta 6) is behaving this way? 任何人都可以告诉我我做错了什么或为什么Xcode(beta 6)这样做?

I presume you mean popToRootViewControllerAnimated in your first snippet, since returnToRootViewController isn't a method on UUNavigationController . 我想你的意思是popToRootViewControllerAnimated在你的第一个片段,因为returnToRootViewController不在一个方法UUNavigationController

Your problem is that popToRootViewControllerAnimated has a return value (the array of view controllers removed from the navigation stack). 您的问题是popToRootViewControllerAnimated有一个返回值(从导航堆栈中删除的视图控制器数组)。 This causes trouble even though you're trying to discard the return value. 即使您尝试丢弃返回值,这也会导致麻烦。

When Swift sees a function/method call with a return value as the last line of a closure, it assumes you're using the closure shorthand syntax for implicit return values. 当Swift看到一个带有返回值的函数/方法调用作为闭包的最后一行时,它假定您正在使用闭包简写语法来获取隐式返回值。 (The kind that lets you write things like someStrings.map({ $0.uppercaseString }) .) Then, because you have a closure that returns something in a place where you're expected to pass a closure that returns void, the method call fails to type-check. (允许你编写someStrings.map({ $0.uppercaseString }) 。)然后,因为你有一个闭包,它返回一个你希望传递一个返回void的闭包的地方,方法调用没有打字检查。 Type checking errors tend to produce bad diagnostic messages — I'm sure it'd help if you filed a bug with the code you have and the error message it's producing. 类型检查错误往往会产生错误的诊断消息 - 我确信如果你提交了一个包含你所拥有的代码的错误以及它产生的错误消息,它会有所帮助。

Anyhow, you can work around this by making the last line of the closure not be an expression with a value. 无论如何,你可以通过使闭包的最后一行不是带有值的表达式来解决这个问题。 I favor an explicit return : 我赞成明确的return

UIView.animateWithDuration(0.3, animations: {
    self.view.layoutIfNeeded()
}, completion: { (complete: Bool) in
    self.navigationController.popToRootViewControllerAnimated(true)
    return
})

You can also assign that popToRootViewControllerAnimated call to an unused variable or put an expression that does nothing after it, but I think the return statement is clearest. 你也可以将popToRootViewControllerAnimated调用分配给一个未使用的变量,或者放一个在它之后什么都不做的表达式,但我认为return语句是最清晰的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM