gpt4 book ai didi

ios - 在 Swift 中选择 UIPickerView 时如何隐藏键盘

转载 作者:行者123 更新时间:2023-11-29 02:13:51 25 4
gpt4 key购买 nike

我有一个 DatePicker,用户可以在其中选择时间和日期字段,键盘首先显示,然后我必须再次单击文本字段才能显示选取器。我想要的是 DatePicker 在第一次尝试时显示。

这是我的代码

@IBOutlet var dateField: UITextField! = UITextField()

@IBOutlet var timeField: UITextField! = UITextField()

@IBAction func userPickDate(sender: UITextField) {

var datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)

}
@IBAction func userPickTime(sender: UITextField) {

var datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Time
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("handleTimePicker:"), forControlEvents: UIControlEvents.ValueChanged)

}

func handleTimePicker(sender: UIDatePicker)
{
var timeFormatter = NSDateFormatter()
timeFormatter.timeStyle = .ShortStyle
timeField.text = timeFormatter.stringFromDate(sender.date)

}



func handleDatePicker(sender: UIDatePicker) {
var timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = .MediumStyle
//timeFormatter.timeStyle = .ShortStyle
dateField.text = timeFormatter.stringFromDate(sender.date)


}

最佳答案

丢弃最后一个答案。我明白你在做什么。这是我发现的:将 UPickerView 对象的声明放在类的范围内而不是本地化到处理程序似乎可行。这是代码。请注意 resignFirstResponder() 的添加。

import UIKit

class ViewController: UIViewController {
var datePickerView : UIDatePicker = UIDatePicker()
var timePickerView : UIDatePicker = UIDatePicker()
@IBOutlet weak var dateField: UITextField!
@IBOutlet weak var timeField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

datePickerView.datePickerMode = UIDatePickerMode.Date
dateField.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)
handleDatePicker(datePickerView)


timePickerView.datePickerMode = UIDatePickerMode.Time
timeField.inputView = timePickerView
timePickerView.addTarget(self, action: Selector("handleTimePicker:"), forControlEvents: UIControlEvents.ValueChanged)
handleTimePicker(timePickerView)


}

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




func handleDatePicker(sender: UIDatePicker) {
var dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
//timeFormatter.timeStyle = .ShortStyle
dateField.text = dateFormatter.stringFromDate(sender.date)
dateField.resignFirstResponder()

}

func handleTimePicker(sender: UIDatePicker) {

var timeFormatter = NSDateFormatter()
timeFormatter.timeStyle = .ShortStyle
timeField.text = timeFormatter.stringFromDate(sender.date)
timeField.resignFirstResponder()

}

}

关于ios - 在 Swift 中选择 UIPickerView 时如何隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28977457/

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