gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:00:56 25 4
gpt4 key购买 nike

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

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

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

enter image description here

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

View Controller

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. 为每个文本字段创建两个 Date 对象,并更新新值当您点击“完成”按钮时为该对象。当文本字段成为第一响应者时,根据该日期更新日期选择器值。
  2. 当文本字段成为第一个时更新日期选择器值回复者,你可以查看这段代码:

    //1. Add UITextFieldDelegate
    class InvoiceViewController: UIViewController, AccessoryToolbarDelegate, UITextFieldDelegate {
    //......

    //2. Set delegate for the textfields
    override func viewDidLoad() {
    super.viewDidLoad()

    dateIssueTextField.delegate = self
    dueDateTextField.delegate = self

    //......
    }

    //3. Update the value of thePicker.date
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"

    if let fullDate = dateFormatter.date(from: textField.text) {
    thePicker.date = fullDate
    }

    return true
    }
    }

关于ios - Swift - 将初始数据从 mysql 加载到 uidatepicker(不是从简单的 uipiker),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48919097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com