Reputation: 23
I have a problem with textField in my custom class of the cell.
Functionality: When user select row, he goes to tipsView, where he can choose some of the Tips and I implemented protocol here, which save that value. It's working right, but, when user put own info into textField, and go to tipsView without choosing smth, he will have old value from protocol.
I have global var in the first view, which saves protocol implementation from the tipsView. But I can't reset this value or either use if else statements, because I didn't see any good solutions for that, if you can tell me where to go, I will be happy to read and learn from you! Thank you!
class Example: UITableViewController {
var selectedCellIdentifier: String = ""
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "tasking") as! tasking
cell.textField.text = selectedCellIdentifier
//
// if cell.textField.text != nil {
//
// selectedCellIdentifier = cell.textField.text!
//
//
// } else {
//
// cell.textField.text = selectedCellIdentifier
// }
// if selectedCellIdentifier != cell.textField.text && cell.textField.text != "" {
// cell.textField.text = cell.textField.text
// selectedCellIdentifier = ""
// } else {
// cell.textField.text = selectedCellIdentifier
// }
return cell
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == segues.goToTips {
let destinationVC = segue.destination as! TaskTips
destinationVC.delegate = self
if let taskName = tasking().textField?.text {
destinationVC.selectedCellValue = taskName
}
}
}
}
extension Example: ChildViewControllerDelegate{
func textTips(string: String) {
selectedCellIdentifier = string
}
Second View
protocol ChildViewControllerDelegate {
func textTips(string: String)
}
class tipsView: UITableViewController {
public var delegate: ChildViewControllerDelegate!
var selectedCellValue = ""
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let indexPath = tableView.indexPathForSelectedRow {
if let name = cells[indexPath.section].cellName?[indexPath.row] {
print(selectedCellValue)
// if name != selectedCellValue {
// self.delegate?.textTips(string: name)
// } else if name == "" {
// self.delegate?.textTips(string: selectedCellValue)
// } else if name == selectedCellValue {
// self.delegate.textTips(string: selectedCellValue)
// }
if name != "" {
self.delegate?.textTips(string: name)
} else {
self.delegate?.textTips(string: selectedCellValue)
}
print(name)
}
tableView.reloadData()
self.navigationController?.popViewController(animated: true)
}
}
}
Upvotes: 0
Views: 153
Reputation: 23
Here is my solution!
created an array, and see if my value in text field contains in array, and job is done !
func textFieldDidEndEditing(_ textField: UITextField) {
let text = selectedCellIdentifier
if cellTips().cellArray.contains(text) {
textField.text = text
} else {
textField.text = textField.text
}
selectedCellIdentifier = ""
}
Upvotes: 0
Reputation: 21
Not sure if I completely understood your requirement but you can conform to UITextFieldDelegate and save the value on shouldChangeCharactersIn method.
Upvotes: 1