简体   繁体   English

让其他函数调用一个函数,但是谁提供了参数值?

[英]Asking some other function call a function, but who provided the argument value?

The code is silly simple. 代码很简单。 There is a button in the view, during run time when user taps the button, the button will move 20 point downwards. 视图中有一个按钮,在运行期间,用户单击该按钮时,该按钮将向下移动20点。

the the viewController has a IBOutlet and a IBAction, both of them are connected to the button in the interface builder viewController有一个IBOutlet和IBAction,它们都连接到界面构建器中的按钮

Here comes the code of the viewControleller 这是viewControleller的代码

class viewController : UIViewController {

    @IBOutlet weak var myButton: UIButton!

    @IBAction func moveMyButton(_ sender: Any) {

        func whatToAnimate() {
            self.myButton.frame.origin.y += 20
        }

        func whatToDoLater(finished: Bool){
            print("finished: \(finished)") //who passed the "true" value to the finished variable
        }

        UIView.animate(withDuration: 0.4, 
                    animations: whatToAnimate, 
                    completion: whatToDoLater)
    }

}

When animate function has been called in the run time, the function whatToDoLater() -> void was passed into the animate method as a argument. 在运行时调用了animate函数后,函数whatToDoLater()-> void被作为参数传递给animate方法。

The string "finished: true" prints in the console when user taps the button 当用户点击按钮时,字符串“ finished:true”在控制台中打印

Question : 问题

Who passed the Boolean value "true" to the whatToDoLater function's argument finished ? 谁将布尔值“ true”传递给whatToDoLater函数的完成参数? I think I didn't do that. 我想我没有那样做。 What's the mechanism behind it? 其背后的机制是什么?

Thanks for your time 谢谢你的时间

The UIView animate method passes the parameter since it is what calls the completion handler. UIView animate方法传递参数,因为它称为完成处理程序。

This pattern is seen with any use of a completion handler. 任何使用完成处理程序都可以看到此模式。 Whatever actually calls the completion handler is also responsible for passing the proper parameter values to the completion handler. 实际调用完成处理程序的任何事物也负责将正确的参数值传递给完成处理程序。

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

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