gpt4 book ai didi

ios - textFieldShouldBeginEditing 在所有文本字段上触发

转载 作者:行者123 更新时间:2023-11-30 12:44:06 27 4
gpt4 key购买 nike

我想实现textFieldShouldBeginEditing,以便我可以为某些文本字段显示带有对话框的弹出窗口,而其他文本字段则为自由文本。

我是这样实现的:

   func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{


if(textField == self.petType)
{
petTypePicker = CZPickerView(headerTitle: "Pet Types", cancelButtonTitle: "Cancel", confirmButtonTitle: "Choose")
petTypePicker?.delegate = self
petTypePicker?.dataSource = self
petTypePicker?.needFooterView = true
petTypePicker?.headerBackgroundColor = #colorLiteral(red: 0.4509803922, green: 0.3529411765, blue: 0.7647058824, alpha: 1)
petTypePicker?.confirmButtonBackgroundColor = #colorLiteral(red: 0.4509803922, green: 0.3529411765, blue: 0.7647058824, alpha: 1)
petTypePicker?.show()
return false
}
else if(textField == self.petBread)
{
breedPicker = CZPickerView(headerTitle: "Breeds", cancelButtonTitle: "Cancel", confirmButtonTitle: "Choose")
breedPicker?.headerBackgroundColor = #colorLiteral(red: 0.4509803922, green: 0.3529411765, blue: 0.7647058824, alpha: 1)
breedPicker?.confirmButtonBackgroundColor = #colorLiteral(red: 0.4509803922, green: 0.3529411765, blue: 0.7647058824, alpha: 1)
breedPicker?.delegate = self
breedPicker?.dataSource = self
breedPicker?.needFooterView = true
breedPicker?.show()
return false
}
else if(textField == self.petColor){
colourPicker = CZPickerView(headerTitle: "Colours", cancelButtonTitle: "Cancel", confirmButtonTitle: "Choose")
colourPicker?.headerBackgroundColor = #colorLiteral(red: 0.4509803922, green: 0.3529411765, blue: 0.7647058824, alpha: 1)
colourPicker?.confirmButtonBackgroundColor = #colorLiteral(red: 0.4509803922, green: 0.3529411765, blue: 0.7647058824, alpha: 1)
colourPicker?.delegate = self
colourPicker?.dataSource = self
colourPicker?.needFooterView = true
colourPicker?.show()
return false
}
else if(textField == self.petDateOfBirth){

let currentDate = Date()

DatePickerDialog().show(title: "DatePickerDialog", doneButtonTitle: "Done", cancelButtonTitle: "Cancel", minimumDate: nil, maximumDate: currentDate, datePickerMode: .date) { (date) in
if let dt = date {
let dateFormatter = DateFormatter()

dateFormatter.dateStyle = .medium

self.petDateOfBirth.text = "\(dateFormatter.string(from: dt) as String)"
}
}

return false
}
else{


return true

}

}

但由于某种原因,发生了这样的情况:

当返回为 false 时,它​​工作得很好,当返回为 true 时,它​​会为所有文本字段触发 textFieldShouldBeginEditing 方法,这使它们全部进入其 if 语句,从而触发所有对话框。

任何想法为什么我会遇到这种行为,请注意所有字段都在堆栈 View 内

谢谢

最佳答案

我很高兴它成功了;)。

使用实例属性 isFirstResponder() 检查您是否单击了正确的文本字段。在您的情况下,将 if 语句修改为 if self.petType.isFirstResponder() 即可解决问题。

关于ios - textFieldShouldBeginEditing 在所有文本字段上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41864106/

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