我有一个textField,我在编辑开始的时候推出了一个pickerView,当Picker出现时触发一个带有textFeld并带有两个按钮的alertView,当这个alertView出现时我的主文本字段结束编辑:但是什么时候击中任何一个我的AlertView的按钮主要文本字段开始编辑,我试图找出为什么这发生从几个小时但没有得到任何线索下面是我的代码(只有部分认为真正负责我的问题):
class ProfileSettingTableViewController: UITableViewController, UITextFieldDelegate {
@IBOutlet var myextField: UITextField! // my main textField
override func viewDidLoad() {
super.viewDidLoad()
myTextField.delegate = self
tableView.tableFooterView = nil
tableView.tableHeaderView = nil
}
// the below function being called once i trigger a button from the picker
func showPickerViewAfterTextFieldEditingEnds {
var tField: UITextField!
func configurationTextField(textField: UITextField!)
{
textField.placeholder = "Enter here"
tField = textField
}
let alert = UIAlertController(title: "Enter Your Name", message: "", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
self.myTextField.resignFirstResponder()
print("close button tapped")
}
alert.addTextFieldWithConfigurationHandler(configurationTextField)
// my below added functions (in the alert) is forcing my textField to start editing in odds attempts (when i hit the button first time then on third time then on 5th time and so on)
alert.addAction(cancelAction)
alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
self.myTextField.text = ""
self.myTextField.text = tField.text
self.myTextField.resignFirstResponder()
}))
self.presentViewController(alert, animated: true, completion: {
})
}
}
func textFieldDidBeginEditing(textField: UITextField){
let leftbutton: UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: nil)
let rightbutton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = leftbutton
self.navigationItem.rightBarButtonItem = rightbutton
}
func textFieldDidEndEditing(textField: UITextField){
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.rightBarButtonItem = nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func nameEditingFinished(sender: AnyObject) {
}
@IBAction func selectNamePressed(sender: AnyObject) {
// my picker function which activates it self when the main TextField is begins editing... and from here the AlertView appears..
}
}
如果有人得到任何线索,请让我知道它对我有帮助
答案 0 :(得分:1)
我不确定为什么会看到代码的这一部分,但似乎您正在尝试使用pickerView作为textField的输入视图。如果是这样,有比使用beginEditing更简单的方法。例如,您可以执行以下操作:
self.picker = UIPickerView()
textfield.inputView = picker
textfield.addTarget(self, action: #selector(textFieldEndEditing), forControlEvents: .EditingDidEnd)
picker.dataSource = self
picker.delegate = self
func textFieldEndEditing(textField : UITextField) {
//Do whatever you would like the callback to do, like:
let row = self.picker.selectedRowInComponent(0)
let value = pickerView(self.picker, titleForRow: row, forComponent: 0)
textField.text = value
}