ios - 如何调用返回的函数(发件人 : UIDatePicker) - Swift

标签 ios xcode swift

我有一个名为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/

相关文章:

objective-c - 如何编写 xcode 用户脚本以使用 NSLocalizedString(<string>, nil) 包围光标所在的字符串

ios - Xcode 中的快速变量检查

ios - *** 由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因 : 'Tried to pop to a view controller that doesn' t exist. '

swift - 如何在 SpriteKit 中改变物体的质量以随着重力下降得更慢

ios - 何时在 UITableViewCell 子类中启动自定义 View ?

ios - Swift & SceneKit 3D 多色四面体

ios - Delphi XE6 firemonkey组件在运行时添加时对齐问题

ios - 无法将 NSData 转换为图片 iphone

ios - 实体关系中的数据属性更改但不触发 nsfetchresultsController

ios - 我应该在 VIPER/MVP iOS 项目中的什么位置放置 Core Location 方法?