gpt4 book ai didi

swift3 - swift 3 日期选择器仅显示日月年

转载 作者:行者123 更新时间:2023-12-02 09:13:19 26 4
gpt4 key购买 nike

我在 swift 3 中使用以下代码来启动日期选择器。

import UIKit


class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var eventStartText: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

eventStartText.delegate = self

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

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: TextField Delegate
func datePickerChanged(sender: UIDatePicker) {
let formatter = DateFormatter()
//formatter.dateStyle = .full
formatter.dateStyle = .long
formatter.timeStyle = .none
eventStartText.text = formatter.string(from: sender.date)

print("Try this at home")
}

func textFieldDidBeginEditing(_ textField: UITextField) {
let datePicker = UIDatePicker()
textField.inputView = datePicker
datePicker.addTarget(self, action: #selector(datePickerChanged(sender:)), for: .valueChanged)

print("This Worked")
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
eventStartText.resignFirstResponder()
return true
}

// MARK: Helper Methods
func closekeyboard() {
self.view.endEditing(true)
}

// MARK: Touch Events
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
closekeyboard()
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

我如何才能只拥有一天、一个月和一年。由于我被要求提供更多详细信息,然后让事情变得清楚,我需要的是 Swift 3 日期选择器仅显示日月年谢谢

最佳答案

如果您只想使用选择器显示日、月和年,只需将 UIDatePickerdatePickerMode 属性设置为 .date

let datePicker = UIDatePicker()
//set datePickerMode to date
datePicker.datePickerMode = .date

textField.inputView = datePicker
datePicker.addTarget(self, action: #selector(datePickerChanged(sender:)), for: .valueChanged)

关于swift3 - swift 3 日期选择器仅显示日月年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43982267/

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