这是我的代码
- @IBOutlet weak var txt1: UITextField!
@IBOutlet weak var txt2: UITextField!
@IBOutlet weak var txt3: UITextField!
@IBOutlet weak var txt4: UITextField!
@IBOutlet weak var txt5: UITextField!
@IBOutlet weak var txt6: UITextField!
@IBOutlet weak var txt7: UITextField!
@IBOutlet weak var picker: UIPickerView!
var category = ["Male", "Female", "Others"]
let datePicker = UIDatePicker()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
picker.isHidden = true
txt3.delegate = self
self.txt3.inputView = picker
//txt3.inputView = category[row]
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return category.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return category[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
txt3.text = category[row]
// category.isHidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField == txt3 {
//resignFirstResponder()
//picker.isHidden = true
}
return true
}
}
@IBAction func txt3(_ sender: Any) {
func showDatePicker(){
//Formate Date
datePicker.datePickerMode = .date
//ToolBar
let toolbar = UIToolbar();
toolbar.sizeToFit()
//done button & cancel button
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action:
Selector(("donedatePicker")))
//let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
// let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.bordered, target: self, action:
Selector("cancelDatePicker"))
toolbar.setItems([doneButton], animated: false)
// add toolbar to textField
txt3.inputAccessoryView = toolbar
// add datepicker to textField
txt3.inputView = datePicker
}
func donedatePicker(){
//For date formate
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
txt3.text = formatter.string(from: datePicker.date)
//dismiss date picker dialog
self.view.endEditing(true)
}
func cancelDatePicker(){
//cancel button dismiss datepicker dialog
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
块引用
答案 0 :(得分:1)
override func viewDidLoad(){
super.viewDidLoad()
picker.isHidden = true
txt3.delegate = self
//self.txt3.inputView = picker // don't do this
self.txt3.inputView = datePicker // do like this.
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.doneButton))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.doneButton)
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
txt3.inputAccessoryView = toolBar
}
func doneButton(){
let selectedDate = datePicker.date
print(selectedDate)
txt3.resignFirstResponder()
}
它会自动打开日期选择器。
答案 1 :(得分:0)
txt3.inputView = picker
并删除
picker.isHidden = true
这应该可以解决问题!
答案 2 :(得分:0)
首先,你添加这个..
txt3.inputView = picker
然后你添加
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField == txt3 {
picker.isHidden = false
}
else {
picker.isHidden = true
}
return true
}