-3

I'm getting data back from secondVC to firstVC and showing it in a text field.

My VC's are FirstVC with a text field and secondVC with a button.

The flow of my VC is when a user clicks on the text field (here text field action Editing Did Begin called) in firstVC then the secondVC will open. And then when the button clicks in secondVC then goes back to firstVC with some data and data will be show in same text field.

So the above is all working fine.

Now I want that the second time when again I click on the text field (now text field contains some data) so then again go to secondVC.

The problem is that now the text field contains data. When I click on it, it doesn't work because of button action property Editing Did Begin.

How to handle this?

Below is my code,

First VC

import UIKit

class firstViewController: UIViewController, UITextFieldDelegate, MyProtocol {

var valueSentFromSecondViewController                   : String?
@IBOutlet weak var myTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}



@IBAction func myTextFieldACTIONWhenEditingDidBegin(_ sender: Any) {
    myTextField.isUserInteractionEnabled = false
    let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController

    secondVC.delegate = self
    self.navigationController?.pushViewController(secondVC, animated: true)
}

func setResultsAfterEvaluation(valueSent: String) {
    self.valueSentFromSecondViewController = valueSent
    print(valueSentFromSecondViewController!)
    myTextField.text = valueSent
    //print(valueSent)

}
}

Second VC

import UIKit

protocol MyProtocol {
func setResultsAfterEvaluation(valueSent: String)

}

class secondViewController: UIViewController {
var delegate            :   MyProtocol?
var sentValue           :   String?
override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func btn(_ sender: Any) {
    sentValue = "Ahtazaz"
    delegate?.setResultsAfterEvaluation(valueSent: sentValue!)
    self.navigationController?.popViewController(animated: true)

}

}
  • Possible duplicate of [How do I check when a UITextField changes?](https://stackoverflow.com/questions/28394933/how-do-i-check-when-a-uitextfield-changes) – Xcoder May 05 '18 at 15:55

1 Answers1

0

I believe this is a more simple way to do what you are trying to achieve, as it doesn't involve protocols and extra functions. If you have any questions, please ask. :)

First View Controller:

import UIKit
//By declaring a variable outside of any class, it is always active in memory and accessible anywhere.
var textFieldText: String = ""

class ViewController1: UIViewController, UITextFieldDelegate {

@IBOutlet weak var textField1: UITextField!

func textFieldDidBeginEditing(_ textField: UITextField) { 
//textField.resignFirstResponder is used to dismiss the keyboard.  By putting it in this function, it hides the keyboard, which prevents users from entering custom text into your text field.
    textField.resignFirstResponder()
    let VC2 = self.storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
    self.navigationController?.pushViewController(VC2, animated: true) 
}

override func viewDidLoad() {
    super.viewDidLoad()
//This links textField1 on your storyboard to the textFieldDidBeginEditing function.
    textField1.delegate = self
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
//Every time the view is about to appear, this is called.  This is where we update the text field's text.
    textField1.text = textFieldText
}
}

Second View Controller:

import UIKit

class ViewController2: UIViewController {

@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!

//If the data you are trying to pass is the button's title, use these two functions.
@IBAction func button1Tapped(_ sender: UIButton) {
    textFieldText = (button1.currentTitle)!
    self.navigationController?.popViewController(animated: true)
}

@IBAction func button2Tapped(_ sender: UIButton) {
    textFieldText = (button2.currentTitle)!
    self.navigationController?.popViewController(animated: true)
} 

//If the data you are trying to pass is not the button's title, use these.
@IBAction func button1Tapped(_ sender: UIButton) {
    textFieldText = "Your Text Here"
    self.navigationController?.popViewController(animated: true)
}

@IBAction func button2Tapped(_ sender: UIButton) {
    textFieldText = "Your Text Here"
    self.navigationController?.popViewController(animated: true)
}
}
TonyStark4ever
  • 848
  • 1
  • 9
  • 24
  • A proper answer explains what was wrong and it explains how the answer fixes the problem. Code-only answers are not nearly as useful. – rmaddy May 05 '18 at 16:40
  • You have done using Segues, I know might be it's very well quite simple But i'm using navigation because of if user don't want data from secondVC then simple it will back by navigation. – Ahtazaz khan May 05 '18 at 16:50
  • textFieldText in Viewcontroller2 getting ERROR. – Ahtazaz khan May 05 '18 at 17:33
  • @Ahtazazkhan Make sure you put the line `var textFieldText: String = ""` OUTSIDE of any class. Put it below the `import UIKit` line in View Controller 1 but above `class: ViewController1`. – TonyStark4ever May 05 '18 at 17:39
  • Fine working, But now the problem is that, Data is going to VC1 using navigating not using Button clicked. – Ahtazaz khan May 05 '18 at 17:45
  • yes it's a big reason :) As in Question, I have already told the scenario is working But the problem is just when i clicked 2nd time on textfield after getting data back, Textfield not working. – Ahtazaz khan May 05 '18 at 17:51
  • @Ahtazazkhan Sorry about that. I forgot to update the code for the second view controller when using a navigation controller. It should be fixed now. – TonyStark4ever May 05 '18 at 17:51
  • Hi, Big news, Now fully functional, Working FINEEEEEEE :) ThankYou very very Much by heart :) – Ahtazaz khan May 05 '18 at 18:06