Swift - 将初始数据从 mysql 加载到 uidatepicker(不是来自简单的 uipiker)
Swift - load initial data from mysql into uidatepicker (not from a simple uipiker)
如何从 mysql 将数据加载到 pickerdate 中?
就像现在一样,我可以在每个文本字段上加载正确的数据。问题是:uidatepicker 只记住 1 个值,而不会加载另一个值。
在图片中我展示了当点击 Date of Issue 文本字段时,它在 pickerView 上显示了 Due Date 的值。正确的应该是:2000年12月25日
Milan 使用相同的功能帮助我创建了选择日期的功能 ==>
视图控制器
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)
}
}
帮手Class
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上读取赋值。
您可以通过两种方式解决问题:
- 为每个文本字段创建两个 Date 对象,并更新新值
当您点击“完成”按钮时为该对象。当文本字段成为第一响应者时,根据该日期更新日期选择器值。
当文本字段成为第一个时更新日期选择器值
回复者,您可以查看此代码:
//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
}
}
如何从 mysql 将数据加载到 pickerdate 中?
就像现在一样,我可以在每个文本字段上加载正确的数据。问题是:uidatepicker 只记住 1 个值,而不会加载另一个值。
在图片中我展示了当点击 Date of Issue 文本字段时,它在 pickerView 上显示了 Due Date 的值。正确的应该是:2000年12月25日
Milan 使用相同的功能帮助我创建了选择日期的功能 ==>
视图控制器
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)
}
}
帮手Class
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上读取赋值。
您可以通过两种方式解决问题:
- 为每个文本字段创建两个 Date 对象,并更新新值 当您点击“完成”按钮时为该对象。当文本字段成为第一响应者时,根据该日期更新日期选择器值。
当文本字段成为第一个时更新日期选择器值 回复者,您可以查看此代码:
//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 } }