我有一个名为handler 的函数。我不知道如何在viewDidLoad
中调用它。这是整个函数:
@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
}
}
这是我在 viewDidLoad
中调用它的方式:
handler(sender:UIDatePicker)
当我把它放在这里时它显示错误是错误的:
Expected member name or constructor call after type name Cannot convert the expression's type
(sender: UIDatePicker.Type)' to type '(UIDatePicker) -> ()
感谢您的时间和耐心,如果您有任何问题或需要任何说明,请发表评论。
最佳答案
您定义的函数将使用 UIDatePicker
的实例作为参数调用,而不是类。例如:
handler(sender:theDatePicker)
此外,您的问题标题表明您可能误解了函数定义。您的 sender:UIDatePicker
是函数的参数,而不是返回值。
关于ios - 如何调用返回的函数(发件人 : UIDatePicker) - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26787729/