gpt4 book ai didi

ios - 文本字段和日期选择器更改值

转载 作者:行者123 更新时间:2023-11-28 23:22:52 25 4
gpt4 key购买 nike

我尝试使用此代码在单击文本字段时获取日期选择器:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var DOBTextField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

DOBTextField.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed))
}

@objc func doneButtonPressed() {
if let datePicker = self.DOBTextField.inputView as? UIDatePicker {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
dateFormatter.locale = Locale.init(identifier: "it_IT")
self.DOBTextField.text = dateFormatter.string(from: datePicker.date)
}
self.DOBTextField.resignFirstResponder()
}
}


extension UITextField {

func addInputViewDatePicker(target: Any, selector: Selector) {

let screenWidth = UIScreen.main.bounds.width

//Add DatePicker as inputView
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216))
datePicker.datePickerMode = .time
datePicker.locale = Locale.init(identifier: "it_IT")
self.inputView = datePicker

//Add Tool Bar as input AccessoryView
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelPressed))
let doneBarButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: selector)
toolBar.setItems([cancelBarButton, flexibleSpace, doneBarButton], animated: false)

self.inputAccessoryView = toolBar
}

@objc func cancelPressed() {
self.resignFirstResponder()
}
}

但是当我再次单击文本字段时,日期选择器的时间在 3 个维度上向前移动。如何解决这个问题?我希望日期选择器上的时间与文本字段上的时间相同。

enter image description here

我尝试实现这段代码,但它崩溃了:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var DOBTextField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

DOBTextField.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed), textFieldText: DOBTextField.text ?? "")
}

@objc func doneButtonPressed() {
if let datePicker = self.DOBTextField.inputView as? UIDatePicker {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
dateFormatter.locale = Locale.init(identifier: "it_IT")
self.DOBTextField.text = dateFormatter.string(from: datePicker.date)
}
self.DOBTextField.resignFirstResponder()
}
}


extension UITextField {

func addInputViewDatePicker(target: Any, selector: Selector , textFieldText: String?) {


let screenWidth = UIScreen.main.bounds.width

//Add DatePicker as inputView
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216))
datePicker.datePickerMode = .time
datePicker.locale = Locale.init(identifier: "it_IT")
self.inputView = datePicker

if textFieldText != nil {
/// Set Textfield date & time
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
dateFormatter.locale = Locale.init(identifier: "it_IT")
datePicker.date = dateFormatter.date(from: textFieldText!)!
}
else {
/// set current date & time
datePicker.date = Date()
}


//Add Tool Bar as input AccessoryView
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelPressed))
let doneBarButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: selector)
toolBar.setItems([cancelBarButton, flexibleSpace, doneBarButton], animated: false)

self.inputAccessoryView = toolBar
}

@objc func cancelPressed() {
self.resignFirstResponder()
}
}

如何解决这个问题?

最佳答案

据我了解,您想在日期选择器打开时显示文本字段的时间。您只需在 DatePicker 初始化时设置日期。

设置DatePicker的日期:

extension UITextField {

func addInputViewDatePicker(target: Any, selector: Selector , textFieldText:String?) {


//Add DatePicker as inputView

//Set date for date Picker
if textFieldText != nil {
/// Set Textfield date & time
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
dateFormatter.locale = Locale.init(identifier: "it_IT")
datePicker.date = dateFormatter.date(from: textFieldText!)!
}
else {
/// set current date & time
datePicker.date = Date()
}

//Add Tool Bar as input AccessoryView
}
}

改变你的viewDidLoad:

override func viewDidLoad() {
super.viewDidLoad()

DOBTextField.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed), textFieldText: DOBTextField.text ?? "")
}

希望您了解必须使用日期选择器设置的内容。如果此代码无法正常工作,请执行文本字段操作(Editing Did Begin)并尝试在 Action 中设置文本字段 inputView。

关于ios - 文本字段和日期选择器更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59372163/

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