我正在使用swift在Xcode中创建一个应用程序,我有一个使用UIDatePicker的文本框,但是现在所有其他文本框都在使用这个日期选择器,因为我希望它们只是一个数字键盘。任何帮助深表感谢。我想成为UIDatePicker的文本框称为“StartDate”,但我还有其他需要成为数字键盘的文本框。我仍然是Swift和XCode的初学者,但就像我说的所有帮助都非常感激。
import UIKit
import EventKit
class ViewController: UIViewController, UITextFieldDelegate {
weak var StartDate: UITextField!
func textFieldDidBeginEditing(textField: UITextField) {
let datePicker = UIDatePicker()
textField.inputView = datePicker
datePicker.datePickerMode = UIDatePickerMode.Date
datePicker.addTarget(self, action: "datePickerChanged:", forControlEvents: .ValueChanged)
}
func datePickerChanged(sender: UIDatePicker) {
let formatter = NSDateFormatter()
formatter.dateStyle = .LongStyle
StartDate.text = formatter.stringFromDate(sender.date)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
// Code to close keyboard that can be used throughout the app
func closeKeyboard() {
self.view.endEditing(true)
}
// Close keyboard on touch anywhere else
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
closeKeyboard()}
override func viewDidLoad() {
super.viewDidLoad()
StartDate.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
答案 0 :(得分:0)
尝试这样的事情:
func textFieldDidBeginEditing(textField: UITextField) {
if textField == StartDate {
let datePicker = UIDatePicker()
textField.inputView = datePicker
datePicker.datePickerMode = UIDatePickerMode.Date
datePicker.addTarget(self, action: "datePickerChanged:", forControlEvents: .ValueChanged)
}else {
//do stuff with the rest of the textFields
}
}