简体   繁体   中英

Pass data from second VC to first VC and also show in textfield Swift4

I'm getting data from second VC to first VC using protocol or delegates, Data is receiving in first VC but the problem is that Data is not showing in Textfield. Here is my Complete Code for understanding. Any Effort is appreciated.

FirstVC class

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()
    // Dispose of any resources that can be recreated.
}


@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!) // Ahtazaz(DATA showing here)
    myTextField.text = valueSentFromSecondViewController //This's the problem, Why not showing here in this this TextField
}
}

Now, SecondVC Class

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) {
    let firstVC = self.storyboard?.instantiateViewController(withIdentifier: "firstViewController") as! firstViewController
    self.navigationController?.pushViewController(firstVC, animated: true)

    sentValue = "Ahtazaz"
    delegate?.setResultsAfterEvaluation(valueSent: sentValue!)
}

}

The steps are Simple to use Delegates for passing the data to previous VC

Second VC:

At the top of VC declare the protocol as follows:

protocol MenuListingDelegate {
func callBackOfMenuSelected(arrSelectedCategory:[Int],isFromWhichPopup:Int)

}

Then inside that define the variable like this

var delegate:MenuListingDelegate?

And then provide the data to the delegate like this. In my case i provide that on click of button before pop View Controller

self.delegate?.callBackOfMenuSelected(strToPass: "Hello")

Now in First VC:

At the top define the Delegate method like this:

class DayDetailVC: UIViewController,MenuListingDelegate {}

And fetch the Data like this

  //MARK:- Menu Listing Delegate

    func callBackOfMenuSelected(strToPass: String) {
     print(strToPass)

    }

Note:- Do not forget to declare the delegate of the secondVC where we use this. secondVC.delegate = self .

Edit Check the following cases

Case 1:- Check the outlets of the myTextField i guess the issue is there. If everything is correct remove the Outlet and the set that again

Case 2:- Still if doesnt work then try setting like this

func setResultsAfterEvaluation(valueSent: String) {
myTextField.text = "\(valueSent)" 
}

Hope this helps.

Edit 2

I have seen you have used pushViewController in the following lines: So you can simply use the following line of code to pass the data to firstVC

In SecondVC add following Code:

 let firstVC = self.storyboard?.instantiateViewController(withIdentifier: "firstViewController") as! firstViewController
firstVC.valueSentFromSecondViewController = "Hello World"
self.navigationController?.pushViewController(firstVC, animated: true)

Now in FirstVC

Use like in viewDidLoad() or anywhere you want

print(valueSentFromSecondViewController) //Hello World

Cheers it Done.

Choose the way you want.

Note:- But i will suggest you to use popViewController instead of pushViewController when returning back from SecondVC -> FirstVC . Rest depends upon your requirements.

Hope this helps.

You are pushing SecondVC. Then in SecondVC you are pushing again FirstVC. I think this is where you are making mistake.

let firstVC = self.storyboard?.instantiateViewController(withIdentifier: "firstViewController") as! firstViewController

You are creating a new instance of FirstVC. Then you push it which is wrong. Call your delegate and then Pop back to previous(FirstVC) controller

Try this code in your button action

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

This should be the correct approach rather than pushing the controller again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM