Swift - 将初始数据从mysql加载到uidatepicker(而不是从简单的uipiker)

时间:2018-02-22 03:20:36

标签: ios swift uidatepicker

如何从mysql将数据加载到pickerdate?

就像现在一样,我可以在每个文本字段上加载正确的数据。问题是:uidatepicker只记住1个值,而不会加载另一个值。

在图片中,我显示当点击发行日期文本字段时,它会在pickerView上显示截止日期的值。正确的应该是:2000年12月25日

enter image description here

米兰帮我创建了选择日期,使用相同的功能==> link to the question

查看控制器

import UIKit
class InvoiceViewController: UIViewController, AccessoryToolbarDelegate {

var thePicker = UIDatePicker()

@IBOutlet weak var dateIssueTextField: UITextField!
@IBOutlet weak var dueDateTextField: UITextField!

func doneClicked(for textField: UITextField) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .short
    dateFormatter.timeStyle = .none

    //show on text field
    dateFormatter.dateFormat = "dd MMM yyyy"
    textField.text = dateFormatter.string(from: thePicker.date)

    //formated to store on mysql
    dateFormatter.dateFormat = "yyyy-MM-dd"
    let finalDate: String = dateFormatter.string(from: thePicker.date)
    print(finalDate)
    textField.resignFirstResponder()
}

func cancelClicked(for textField: UITextField) {
    textField.resignFirstResponder()
}

func setUpTextFieldPicker(textField: UITextField, date: String) {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat =  "yyyy-MM-dd"

    if let fullDate = dateFormatter.date(from: date) {
        thePicker.date = fullDate
        dateFormatter.dateFormat = "dd MMM yyyy"
        let finalDate: String = dateFormatter.string(from: fullDate)
        textField.text = finalDate
    }


    textField.inputView = thePicker
    let toolbar = AccessoryToolbar(for: textField)
    toolbar.accessoryDelegate = self
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.thePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216))
    self.thePicker.backgroundColor = UIColor.white
    self.thePicker.datePickerMode = UIDatePickerMode.date

    //data will be loaded from an API to a server. Hard Coded now just for testing
    setUpTextFieldPicker(textField: dateIssueTextField, date:"2000-12-25" )
    setUpTextFieldPicker(textField: dueDateTextField, date:"2010-05-03")
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
}
}

助手类

import UIKit

protocol AccessoryToolbarDelegate: class {
func doneClicked(for textField: UITextField)
func cancelClicked(for textField: UITextField)
}

class AccessoryToolbar: UIToolbar {

fileprivate let textField: UITextField

weak var accessoryDelegate: AccessoryToolbarDelegate?

init(for textField: UITextField) {
    self.textField = textField
    super.init(frame: CGRect.zero)

    self.barStyle = .default
    self.isTranslucent = true
    self.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
    self.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneClicked))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelClicked))
    self.setItems([cancelButton, spaceButton, doneButton], animated: false)
    self.isUserInteractionEnabled = true

    textField.inputAccessoryView = self
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

@objc fileprivate func doneClicked() {
    accessoryDelegate?.doneClicked(for: self.textField)
}

@objc fileprivate func cancelClicked() {
    accessoryDelegate?.cancelClicked(for: self.textField)
}
}

小注意:我认为应该在viewWillAppear上读取和分配数据。

1 个答案:

答案 0 :(得分:1)

您可以通过两种方式解决问题:

  1. 为每个文本字段创建两个Date对象,并更新新值 当您点击完成按钮时该对象。当文本字段成为第一个响应者时,根据该日期更新日期选择器值。
  2. 当文本字段成为第一个时,更新日期选择器值 响应者,您可以查看此代码:

    static constraints = {
        embedCode(nullable:true, blank:true, unique:false, display:true, supportsToUrl:true)
    }