gpt4 book ai didi

ios - 如何快速在同一个 UIView 中创建自定义 UI Date PickerViews

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:06 25 4
gpt4 key购买 nike

我试图在同一个 viewController 上有两个不同的日期选择器 View ,并让它们代表不同的数据。我尝试了不同的方法来做到这一点,但是两个日期选择器 View 都显示相同的数据,但不能把它们放在一起。我是快速编程的新手。任何帮助都会很棒!

 @IBOutlet weak var EventStart: UITextField!
var datepicker = UIDatePicker()
var datepicker2 = UIDatePicker()
@IBOutlet weak var EventEnd: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

createDatePicker()
Event.delegate = self
eventName.delegate = self
// Do any additional setup after loading the view.
}
func createDatePicker(){

datepicker.datePickerMode = .dateAndTime
datepicker2.datePickerMode = .dateAndTime
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneButton], animated: false)
EventEnd.inputAccessoryView = toolbar
EventEnd.inputView = datepicker2
EventStart.inputAccessoryView = toolbar
EventStart.inputView = datepicker
}

func donePressed(){
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
EventStart.text = dateFormatter.string(from: datepicker.date)
EventEnd.text = dateFormatter.string(from: datepicker2.date)
print(EventStart)
print(EventEnd)
self.view.endEditing(true)
}

最佳答案

@IBOutlet weak var EventStart: UITextField!
var datepicker = UIDatePicker()
var datepicker2 = UIDatePicker()
@IBOutlet weak var EventEnd: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

self.addDatePicker(datepicker, textfield:EventStart , selector: #selector(ViewController.donePressed))
self.addDatePicker(datepicker2 , textfield:EventEnd ,selector: #selector(ViewController.donePressed2))

// Do any additional setup after loading the view.
}


func addDatePicker(_ datepicker : UIDatePicker, textfield: UITextField, selector: Selector) {


let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action:selector)
toolbar.setItems([doneButton], animated: false)


textfield.inputAccessoryView = toolbar
textfield.inputView = datepicker

}

func donePressed(){
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
EventStart.text = dateFormatter.string(from: datepicker.date)
print(EventStart)
self.view.endEditing(true)
}

func donePressed2(){
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
EventEnd.text = dateFormatter.string(from: datepicker2.date)
print(EventEnd)
self.view.endEditing(true)
}

关于ios - 如何快速在同一个 UIView 中创建自定义 UI Date PickerViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45435401/

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