如何调用返回的函数(sender:UIDatePicker) - Swift

时间:2014-11-06 19:23:27

标签: ios xcode swift

我有一个名为处理程序的函数。我不知道如何在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)

当我把它放在这里时显示错误是错误:

  

类型名称后的预期成员名称或构造函数调用   无法转换表达式的类型(sender: UIDatePicker.Type)' to type '(UIDatePicker) -> ()

感谢您的时间和耐心,如果您有任何疑问或需要澄清,请发表评论。

3 个答案:

答案 0 :(得分:1)

您定义的函数将使用UIDatePicker的实例作为参数调用,而不是使用类调用。例如:

handler(sender:theDatePicker)

此外,您的问题标题表明您可能会误解功能定义。您的sender:UIDatePicker是函数的参数,而不是返回值。

答案 1 :(得分:1)

我想你打算打电话:

handler(sender: theDatePicker)

这会传递你的UIDatePicker对象而不是UIDatePicker类。

答案 2 :(得分:0)

调用该函数时,您不需要指定类型。所以,只需handler(sender)即可。