UIDatePickerView上的SelectRow

时间:2018-12-11 16:41:40

标签: swift uipickerview datetimepicker swift4.2

我正在搜索如何选择pickerview值。我的pickerView是一个时间选择器View,它返回08:25、06:37、03:24等...

我找到了一个名为SelectRow的函数,但是当我尝试使用它时,却收到以下消息:

  

pickerView的值没有成员“ selectRow”

要使用它,我必须做什么?

我不需要使用UIPickerViewDelegateUIPickerViewData,因为我不需要像numbersOfRow这样的功能。

这是我的代码:

    //
//  Addhoraire.swift
//  School Companion
//
//  Created by Cédric Loneux on 08/12/2018.
//  Copyright © 2018 Cédric Loneux. All rights reserved.
//

import UIKit

class Addhoraire: ViewController {
    @IBOutlet weak var nomDuCoursTextField: UITextField!

    @IBOutlet weak var debutTextField: UITextField!
    @IBOutlet weak var finTextField: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let tap = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
        view.addGestureRecognizer(tap)

    }

    @objc func hideKeyboard() {
        view.endEditing(true)
        debutTextField.backgroundColor = UIColor.white
        finTextField.backgroundColor = UIColor.white
        debutTextField.textColor = UIColor.black
        finTextField.textColor = UIColor.black
    }

    var hourSelected : String = ""

    @IBAction func debutHoraire(_ sender: UITextField) {
        finTextField.backgroundColor = UIColor.white
        finTextField.textColor = UIColor.black
        debutTextField.backgroundColor = UIColor(red:0.13, green:0.36, blue:1.00, alpha:1.0)
        debutTextField.textColor = UIColor.white
        hourSelected = "début"

        let datePickerView:UIDatePicker = UIDatePicker()
        datePickerView.datePickerMode = UIDatePicker.Mode.time
        debutTextField.inputView = datePickerView
        datePickerView.addTarget(self, action: #selector(Addhoraire.datePickerFromValueChanged), for: UIControl.Event.valueChanged)
    }


    @IBAction func finHoraire(_ sender: UITextField) {
        debutTextField.backgroundColor = UIColor.white
        debutTextField.textColor = UIColor.black
        finTextField.backgroundColor = UIColor(red:0.13, green:0.36, blue:1.00, alpha:1.0)
        finTextField.textColor = UIColor.white
        hourSelected = "fin"

        let datePickerView2:UIDatePicker = UIDatePicker()
        datePickerView2.datePickerMode = UIDatePicker.Mode.time
        finTextField.inputView = datePickerView2
        datePickerView2.addTarget(self, action: #selector(Addhoraire.datePickerFromValueChanged), for: UIControl.Event.valueChanged)
    }

    @objc func datePickerFromValueChanged(sender:UIDatePicker) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm"
        if hourSelected == "début" {
            debutTextField.text = dateFormatter.string(from : sender.date)
        }
        else {
            finTextField.text = dateFormatter.string(from : sender.date)
        }
    }

    @IBAction func btnRetour(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }

    @IBAction func btnValider(_ sender: UIButton) {
        if (nomDuCoursTextField.text!.isEmpty || debutTextField.text!.isEmpty || finTextField.text!.isEmpty) {
            let alert = UIAlertController(title: "Impossible d'ajouter le cours", message: "Veillez renseigner tout les champs obligatoires", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }




    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

1 个答案:

答案 0 :(得分:0)

我已经将它添加到函数中了,它可以正常工作

if debutTextField.text != "" {
        // récupérer l'heure complète en string, scinder en deux string ( heure et minutes), convertir les deux en int pour les mettre en components
        let heureComplete : String = debutTextField.text!
        print(heureComplete)
        let strheure : String = heureComplete.substring(toIndex: 2)
        let heure : Int = Int(strheure)!
        let strminutes : String = heureComplete.substring(fromIndex: 3)
        let minutes : Int = Int(strminutes)!

        let calendar: NSCalendar = NSCalendar(calendarIdentifier:  .gregorian)!
        let components: NSDateComponents = NSDateComponents()
        components.hour = heure
        components.minute = minutes
        let defaultDate: NSDate = calendar.date(from: components as DateComponents)! as NSDate
        datePickerView.date = defaultDate as Date
    }