2

I have a textfield in my tableview cell. When I am entering something and click on the submit button, I want to save the text in a struct to use it in other view controller. But the problem is , when I am clicking the submit button for the first time, my textFieldDidEndEditing function is not calling so I am unable to assign the value to the struct.

My code is-

   func textFieldDidEndEditing(_ textField: UITextField) 

{
        let position: CGPoint = textField.convert(CGPoint.zero, to: self.tableView)
       
        if let indexPath = self.tableView.indexPathForRow(at: position)
        {
           
                
           if let cell: InsuranceInformationTableViewCell = self.tableView.cellForRow(at: indexPath) as? InsuranceInformationTableViewCell{
            
            if textField == cell.policyHolderFullNameTextField{
                insuranceModel.policuHolderFullName = textField.text
            }
                        
        }
}
}

SubmitButtonAction-

    @IBAction func SubmitButtonAction(_ sender: UIButton) {   
 
            if (insuranceModel.policuHolderFullName == nil) {
               showAlert(withTitle: "Data Missing", withMessage: "Policy holder fullName field cannot be empty")
               return
           }
                        
            let encoder = JSONEncoder()
           if let encoded = try? encoder.encode(insuranceModel) {
               let defaults = UserDefaults.standard
               defaults.set(encoded, forKey: "SavedInsuranceInformation")
            
        self.navigationController?.pushViewController(StockAndBillPreviewViewController.instantiateStockAndBillPreviewViewController(), animated: true)
 }
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 case TableSection.insuranceType.rawValue:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "InsuranceInformationTableViewCell", for: indexPath) as? InsuranceInformationTableViewCell else {
                return UITableViewCell()
            }
            
            cell.policyHolderFullNameTextField.delegate = self
}

When I am clicking on the submit button it is showing me the error message. Here i don't want to use "UITextField, shouldChangeCharactersIn range". Becausei have lots of textfields in my tableview cell as it is a medical form.

How to resolve this issue ? Can anyone help me?

Tapan Raut
  • 850
  • 6
  • 17
  • 2
    Add this line inside the button action self.view.endEditing(true) – Raja Kishan Feb 06 '21 at 14:50
  • Probably textField does not resign firt responder when you tap the button. That's why I prefer creating a specific IBAction that I connect to both events: Did End on Exit and Editing Did End. – claude31 Feb 06 '21 at 15:33

2 Answers2

0

1.one of most common reason this might happen is that you might not be assigning the text view delegate method. So just add "UITextViewDelegate" as conformance to your table View Cell and then set the delegate in storyboard. 2. if you have done the first method and it is still not working then you can follow the most basic way to get whatever there is in text field when you click your submit button. at anypoint in your code you can access the text in textfield through "textfield.text", just create an outlet of your text field and use youroutletname.text in submit button to save in struct.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Chahat
  • 11
  • 1
0

I am not sure that textFieldDidEndEditing is calling in this case. I use the following approach:

(1) define custom cell with UITextView and make cell delegate for UITextViewDelegate textViewDidChange method

(2) create custom cell delegate protocol to send text changes down to TableViewController

(3) make TableViewController delegate for custom cell and record all changes for UITextView text inside the controller