如何将文本字段占位符的值设置为时间选择器的选定值

时间:2019-07-04 08:53:10

标签: swift xcode

在“ func can()”上,弹出一个文本框,我们单击它并显示一个时间选择器。我想将文本字段占位符值设置为所选选择器的值

@IBAction func can(_ sender: UIButton) {
    let alertController : UIAlertController = UIAlertController(title: "", message: "Confirm Order ?", preferredStyle: UIAlertController.Style.alert)
    alertController.addTextField
    {
        (textField : UITextField!) -> Void in
        textField.placeholder = "Expected Time"
        textField.addTarget(self, action: #selector(self.myTargetFunction), for: .touchDown)
        let okAction = UIAlertAction(title: "CANCEL", style: UIAlertAction.Style.destructive) {
        (UIAlertAction) in
        }
        let CANAction = UIAlertAction(title: "SUBMIT", style: UIAlertAction.Style.default) {
        (UIAlertAction) in
        // self.actionCancelOrderApiCall()
        }
        alertController.addAction(okAction)
        alertController.addAction(CANAction)
        self.present(alertController, animated: true, completion: nil)
    }
}

@objc func myTargetFunction(textField: UITextField) {
    var datePicker : UIDatePicker = UIDatePicker(frame: CGRect(x: 0,y: 500, width: self.view.frame.size.width, height: 250))
    datePicker.datePickerMode = .time
    datePicker.backgroundColor = UIColor.white
    if datePicker.isHidden == false{
        tblV.isHidden = true
        atchment1.isHidden = true
    }
    self.view.addSubview(datePicker)
}

1 个答案:

答案 0 :(得分:0)

以下更改应使其起作用

textField.addTarget(self, action: #selector(self.myTargetFunction(sender:)), for: .touchDown)

将方法签名更改为

@objc func myTargetFunction(sender: UITextField)

myTargetFunction()内部,

sender.placeholderText = datePicker.date