gpt4 book ai didi

ios - textField beginEditing被多次调用,无法查出问题

转载 作者:行者123 更新时间:2023-11-29 00:59:01 24 4
gpt4 key购买 nike

我有一个 textField,当编辑开始时,当 Picker 出现时,我在其中放置了一个 pickerView,我触发了一个带有两个按钮的 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..

}

}

如果有人得到任何线索,请告诉我,这对我很有帮助

最佳答案

我不确定为什么会发生这种情况,看看这部分代码,但似乎您正在尝试使用 pickerView 作为 textField 的输入 View 。如果是这样,有比使用 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

}

关于ios - textField beginEditing被多次调用,无法查出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37258858/

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