Why does textfieldDidEndEditing getting called immediately after textfieldBeginEditing in iOS 13? Works Fine in iOS lower versions

Viewed 220

I want to edit the textfield's text and save it whenever user slides on to left on the collection view cell and clicks on rename.So I wrote cell.textfield.becomefirstresponder() whenever rename is clicked..Then In case of handling textfield delegate methods, First textDidBeginEditing is called , then without even doing anything, textfieldDidEndEditing is getting called resigning the responder status.It is happening only in iOS 13

public func textFieldDidBeginEditing(_ textField: UITextField) {
        self.emptyView.isHidden = true
    }

    public func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
        setChangedValues(selectedTextField: textField)
    }

    public func textFieldShouldReturn(_ textField: UITextField) -> Bool {

        //setChangedValues(selectedTextField: textField)
        textField.resignFirstResponder()
        return true
    }

    public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {


        if range.location == 0 && string == " " {
            return false
        }

        //Fix:- when u remove the text completely and still textfield contains the last character
        if range.location == 0 && string == "" {
            textField.text = ""
        }

        //Setting the limit
        var maxLimit = 50
        if textField.tag == 2 {
            maxLimit = 15
        }
        let newLength = (textField.text?.count)! + string.count - range.length

        if newLength <= maxLimit {
            //If new character is typed

        } else {
            //If the entered character is more than the limit, return false
            return newLength<=maxLimit
        }

        return true
    }

    func setChangedValues(selectedTextField: UITextField) {

        if (selectedTextField.text?.isEmpty)! {
            assetsTableView.reloadData()
        } else {

            var assetModels = Array<FHTeamAssetsModel>()
            if isEdit {
                //Both are different, please update
                if selectedTextField.text != assetsList[selectedAsset!][WPEConstants.ASSET__NAME]! {
                    //Getting the asset based on asset list selection
                    let asset = FHBase.getAssetModel(id: assetsList[selectedAsset!][WPEConstants.ASSET__ID]!)
                    asset.name = selectedTextField.text
                    FHBase.updateTeamAssets(model: asset)

                }
                if selectedPath > 0 {

                    //Getting the updated sub asset list
                    assetModels = FHBase.getTeamAssetsModels(assetId: assetsPathArray[selectedPath][WPEConstants.ASSET_ID])
                } else {
                    //Getting the updated asset list(without assetId)
                    assetModels = FHBase.getTeamAssetsModels(assetId: nil)
                }

            } else {
            if selectedPath > 0 {
                //Creating a new sub-asset, with assetId
                FHBase.shared.createAssetsModel(name: selectedTextField.text, assetId: assetsPathArray[selectedPath][WPEConstants.ASSET_ID], assetName: assetsPathArray[selectedPath][WPEConstants.ASSET_NAME])

                //Getting the updated sub asset list
                assetModels = FHBase.getTeamAssetsModels(assetId: assetsPathArray[selectedPath][WPEConstants.ASSET_ID])


            } else {
                //Creating a new asset(without assetId)
                FHBase.shared.createAssetsModel(name: selectedTextField.text, assetId: nil, assetName: nil)
                //Getting the updated asset list(without assetId)
                assetModels = FHBase.getTeamAssetsModels(assetId: nil)
            }

            }

            //Creating asset list
            self.createAssetsList(assetModels: assetModels)

            assetsTableView.reloadData()
            self.isEdit = false
        }

        self.updateEmptyStateUI()
    }
1 Answers

Check whether somebody disable textField!

Related