简体   繁体   English

如何调用返回的函数(发送方:UIDatePicker)-Swift

[英]How to call a function that returns (sender: UIDatePicker) - Swift

I have a function named handler . 我有一个名为handler的函数 I don't know how to call it in viewDidLoad . 我不知道如何在viewDidLoad调用它。 Here is the whole function: 这是整个功能:

@IBOutlet weak var theDatePicker: UIDatePicker!

    func handler(sender: UIDatePicker) {
        var timeFormatter = NSDateFormatter()
        timeFormatter.timeStyle = NSDateFormatterStyle.ShortStyle

        var strDate = timeFormatter.stringFromDate(theDatePicker.date)
        let alert = UIAlertController(title: "WAKE UP", message:nil, preferredStyle: UIAlertControllerStyle.Alert);


        alert.message = "Time to wake up " + strDate;


        let dismissHandler = {
            (action: UIAlertAction!) in
            self.dismissViewControllerAnimated(true, completion: { () -> Void in
            })
        }

        alert.addAction(UIAlertAction(title: "I'm Up!", style: .Default, handler: dismissHandler))
        presentViewController(alert, animated: true) { () -> Void in

        }

    }

Here is how I called it in viewDidLoad : 这是我在viewDidLoad称呼它的方式:

handler(sender:UIDatePicker)

It shows errors when I put that in here are the errors: 当我在这里输入错误时,它显示了错误:

Expected member name or constructor call after type name Cannot convert the expression's type (sender: UIDatePicker.Type)' to type '(UIDatePicker) -> () 类型名称后的预期成员名称或构造函数调用无法将表达式的类型(sender: UIDatePicker.Type)' to type '(UIDatePicker) -> ()

Thank you for your time and patience, if you have any questions or need any clarification please comment. 感谢您的时间和耐心,如果您有任何疑问或需要澄清,请发表评论。

Your function as you've defined it would be called with an instance of UIDatePicker as a parameter, not with the class. 您定义的函数将以UIDatePicker的实例作为参数而不是使用类来调用。 For example: 例如:

handler(sender:theDatePicker)

Also, your question title indicates that you might misunderstand function definitions. 另外,您的问题标题表明您可能会误解函数定义。 Your sender:UIDatePicker is a parameter of your function, not a return value. 您的sender:UIDatePicker是函数的参数,而不是返回值。

I think you meant to call: 我想你打算打电话给:

handler(sender: theDatePicker)

This passes it your UIDatePicker object rather than the UIDatePicker class. 这会将您的UIDatePicker对象而不是UIDatePicker类传递给它。

When calling the function, you don't need to specify the type. 调用函数时,无需指定类型。 So, just handler(sender) will do. 因此,只需handler(sender)

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

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