-2

I have a label that gets info from the previous view controller as follows

  @IBOutlet weak var textField: UITextField!
   var label = String()

  func textFieldDidBeginEditing(_ textField: UITextField) {
        self.textField.text = String(self.label.dropLast())
    }

Here is the scenario:

textField on loading: Apple (edit Icon)
textField on editing first time: Apples are healthy
textField on tapping again: Apple 

Everything is fine when I start editing textField but once I tapped outside or go to another field and come back to the textField, all the editing is gone. How to prevent this from happening?

Coder221
  • 1,403
  • 2
  • 19
  • 35
  • Have you added any other textfield delegate methods? – RajeshKumar R Mar 19 '19 at 08:06
  • Only, textFieldDidEndEditing for sake of tint color. – Coder221 Mar 19 '19 at 08:10
  • Can you give an example with actual strings of what's happening? – TMob Mar 19 '19 at 08:18
  • check the `clear when editing begins` property in storyboard if it's marked or not – Arash Etemad Mar 19 '19 at 08:18
  • What do you want to achieve exactly, i.e. using _textField's_ delegate? – Shubham Bakshi Mar 19 '19 at 08:27
  • The only reason I used delegates is for adding edit icon, which will be deleted on tap and I can edit. – Coder221 Mar 19 '19 at 08:34
  • @TMob, please see the edited post – Coder221 Mar 19 '19 at 08:35
  • 1
    Well every time you enter the text-field it will set its content to the content of "label" with one character removed. What are you trying to achieve? – TMob Mar 19 '19 at 08:38
  • I am trying to keep my edit without being reset. I cannot think of any way to do it. Is there a way to do it? – Coder221 Mar 19 '19 at 08:40
  • I mean what are you trying to achieve with setting the text of the textfield to label.dropLast? Do you want to fill it with the value of a previous viewController? – TMob Mar 19 '19 at 08:44
  • so you want to show/hide edit icon depending on whether the textField is first responder or not? – Shubham Bakshi Mar 19 '19 at 08:44
  • @ShubhamBakshi Exactly, that is what I want to do and I don't know whether I am using the right delegate – Coder221 Mar 19 '19 at 08:49
  • @TMob, Yes I want to fill it with value of previous viewController with edit icon and remove that edit icon, when tapped on this new ViewController. – Coder221 Mar 19 '19 at 08:57
  • Okay, so, is there a _single textField_ in your ViewController or is there a tableView with each cell having their own _textFields_ (like you are resting the same cell for the tableView) – Shubham Bakshi Mar 19 '19 at 09:00
  • Only single textField and I don't have any tableView – Coder221 Mar 19 '19 at 09:01
  • Then you can just toggle the visibility of the _edit icon_ using `isHidden` property inside `textFieldDidBeginEditing(_ textField: UITextField)` and `textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason)` – Shubham Bakshi Mar 19 '19 at 09:05
  • But why are you using this `self.textField.text = String(self.label.dropLast())` , I still wonder if you just want to toggle the visibility of the edit icon – Shubham Bakshi Mar 19 '19 at 09:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190288/discussion-between-coder221-and-shubham-bakshi). – Coder221 Mar 19 '19 at 09:07

1 Answers1

2

I think it's because textFieldDidBeginEditing is called each time you tap on the textField, so the text is reset to your label's text.

If it is a one-time-only assignment, you should consider doing it in viewDidLoad. This will also allow you to keep your initial label value if you don't want it changed.

You can also assign the text to your label property in textFieldDidEndEditing.

Mango
  • 187
  • 1
  • 8