I have three viewcontrollers. here i want to add second viewcontroller textfield value to first viewcontroller tableview label from third viewcontroller done button. I am able to add second viewcontroller textfield to first viewcontroller label using delegate while popviewcontroller, but i am unable to add from third viewcontroller button. please help me in the code.
here is my code:
this is my firstVC code:
import UIKit
class EmployeeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, EmployeeDelegateProtocol {
var namesArray = [String]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func sendDataToEmployeeViewController(myData: String) {
self.namesArray.append(myData)
tableView.reloadData()
print(namesArray)
print("in delegate method \(namesArray)")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return namesArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EmployeeTableViewCell
cell.empName.text = namesArray[indexPath.row]
return cell
}
@IBAction func addButtonClicked(_ sender: Any) {
let empProfileVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeProfileViewController") as! EmployeeProfileViewController
empProfileVC.delegate = self
self.navigationController?.pushViewController(empProfileVC, animated: true)
}
}
this is my secondVC code:
while using pop it is working
import UIKit
protocol EmployeeDelegateProtocol {
func sendDataToEmployeeViewController(myData: String)
}
class EmployeeProfileViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate,UIImagePickerControllerDelegate {
var delegate: EmployeeDelegateProtocol?
@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
@objc func saveButtonClicked(){
print("button clicked")
self.delegate?.sendDataToEmployeeViewController(myData: firstNameTextField.text!)
//self.navigationController?.popViewController(animated: true)
}
}
but i want to send the value from thirdVc done button:
import UIKit
class EmployeeProfilecreatedPopUpViewController: UIViewController {
var cellProfile: EmployeeProfileViewController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func doneButtonTapped(_ sender: Any) {
print("done tapped")
let empVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeViewController") as! EmployeeViewController self.navigationController?.pushViewController(empVC, animated: true)
}
}
please suggest me how to send from thirdvc done button.