我很快就是新人。在执行此应用程序登录屏幕时,将登录。选择“已经有帐户注册”。您可以注册查看。在尝试开发选择器视图的注册中。这是开发选择器视图时出现的两个问题
1)选择器视图应隐藏在加载视图控制器上。但是当视图控制器出现时,选择器视图是否可见?
2)在选择日期选择器视图上的完成按钮时,在出生日期中,应用程序崩溃。但是它应该在文本字段中显示相应的日期吗?
func showDatePicker(){
//Formate Date
datePicker.datePickerMode = .date
//ToolBar
let toolbar = UIToolbar();
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: "donedatePicker")
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.bordered, target: self, action: "cancelDatePicker")
toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)
dateTextField.inputAccessoryView = toolbar
dateTextField.inputView = datePicker
}
func donedatePicker(){
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
dateTextField.text = formatter.string(from: datePicker.date)
self.view.endEditing(true)
}
func cancelDatePicker(){
self.view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerView.delegate=self
pickerView.dataSource=self
genderTextField.inputView=pickerView
showDatePicker()
}
如何避免此次崩溃。您可以从此链接https://drive.google.com/file/d/1a6Pmw2gVDwLP9grL8tG72NBx-tKwZozI/view?usp=sharing
下载项目答案 0 :(得分:0)
undefined
错误就在这里。当视图加载到override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerView.delegate=self
pickerView.dataSource=self
genderTextField.inputView=pickerView
showDatePicker()
}
时,日期选择器会自动显示。因此,您必须添加一个单击操作以显示选择器视图并在其中调用它。
例如
viewDidLoad
此外,您的操作必须设置为#selector
@objc fileprivate func showIt(_ sender:UIButton){
showDatePicker()
}
更改为
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: "donedatePicker")
对于错误#1,你必须以某种方式犯let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: #selector(donedatePicker))
firstResponder
,你必须辞职。
您可以为textFieldDelegate添加此功能。
UITextField
答案 1 :(得分:0)
你错过了很少的东西,加载我更新了代码
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate=self
pickerView.dataSource=self
genderTextField.inputView = pickerView
genderTextField.delegate = self
dateTextField.delegate = self
}
分配文本字段委托并实现textFieldShouldBeginEditing函数以显示日期选择器
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField.tag == 1 { //Date Text Field
showDatePicker()
}
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
我已经从故事板中分配了日期文本字段的标记值,并从故事板中隐藏了选择器视图。它将解决你的两点
答案 2 :(得分:0)
首先,您需要向UITextField提供委托,即
dateTextField.delegate = self
然后你需要用UITextFieldDelegate方法确认注册类,即
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == dateTextField {
self.showDatePicker()
}
}
使用上面的代码可能对您有帮助。
答案 3 :(得分:0)
你应该在日期文本字段点按这个showDatePicker()
。
func textFieldDidBeginEditing(_ textField: UITextField) {
let datePicker = UIDatePicker()
if startDateTxt == textField{
textField.inputView = datePicker
datePicker.addTarget(self, action: #selector(showDatePicker()), for: .valueChanged)
}
}
和第二个错误:
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: #selector(donedatePicker))