简体   繁体   中英

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

I have a function named handler . I don't know how to call it in 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 :

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) -> ()

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. 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.

I think you meant to call:

handler(sender: theDatePicker)

This passes it your UIDatePicker object rather than the UIDatePicker class.

When calling the function, you don't need to specify the type. So, just handler(sender) will do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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