如何在 swift 中使用委托将第二个视图控制器文本字段值添加到第三个视图控制器按钮中的第一个视图控制器标签



>我有三个视图控制器。 在这里,我想将第二个视图控制器文本字段值添加到第一个视图控制器表视图标签从第三个视图控制器完成按钮。我可以在popviewcontroller时使用委托将第二个视图控制器文本字段添加到第一个视图控制器标签,但我无法从第三个视图控制器按钮添加。请在代码中帮助我。

这是我的代码:

这是我的第一个VC代码:

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)
}
}

这是我的第二个VC代码:

使用POP时它正在工作

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)
}
}

但我想从 ThirdVc 完成按钮发送值:

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)
}
}

请建议我如何从第三方完成按钮发送。

FirstViewController

import UIKit
class EmployeeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, EmployeeDelegateProtocol {
    //MARK:- ----- Outlets and variables ------
    @IBOutlet weak var tableView: UITableView!
    var namesArray = [String]()

    //MARK:- ----- View lifecycle -----
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }
    //MARK:- ----- TableView Datasource ------
    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
    }
    //MARK:- ----- Button Actions ------
    @IBAction func addButtonClicked(_ sender: Any) {
        let empProfileVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeProfileViewController") as! EmployeeProfileViewController
        empProfileVC.delegate = self
        self.navigationController?.pushViewController(empProfileVC, animated: true)
    }
    //MARK:- ----- Employee delegate ------
    func popController(_ controller: UIViewController, withData: String) {
        self.namesArray.append(myData)
        tableView.reloadData()
        print(namesArray)
        print("in delegate method (namesArray)")
        controller.navigationController?.popViewController(animated: false)
    }
}

第二视图控制器

import UIKit
protocol EmployeeDelegateProtocol {
    func popController(_ controller: UIViewController, withData: String)
}
 class EmployeeProfileViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, UIImagePickerControllerDelegate, EmployeeProfileDelegate {
    //MARK:- ----- Outlets and variables ------
    var delegate: EmployeeDelegateProtocol?
    @IBOutlet weak var firstNameTextField: UITextField!
    @IBOutlet weak var tableView: UITableView!

    //MARK:- ----- View lifecycle -----
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    //MARK:- ----- Button Actions ------
    @objc func saveButtonClicked() {
        let popUpVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeProfilecreatedPopUpViewController") as! EmployeeProfilecreatedPopUpViewController
        popUpVC.delegate = self
        self.navigationController?.pushViewController(popUpVC, animated: true)
        // OR
        self.present(popUpVC, animated: true) { }
        /*print("button clicked")
        self.delegate?.sendDataToEmployeeViewController(myData: firstNameTextField.text!)*/
        //self.navigationController?.popViewController(animated: true)
    }

    //MARK:- ----- Employee profile delegate ------
    func popController(_ controller: UIViewController) {
        self.delegate?.popController(self, withData: firstNameTextField.text!)
        controller.navigationController?.popViewController(animated: false)
        // OR
        controller.dismiss(animated: true) {
            self.delegate?.popController(self, withData: self.firstNameTextField.text!)
        }
    }
}

第三视图控制器

import UIKit
protocol EmployeeProfileDelegate {
    func popController(_ controller: UIViewController)
}
class EmployeeProfilecreatedPopUpViewController: UIViewController {
    //MARK:- ----- Outlets and variables ------
    var delegate: EmployeeProfileDelegate?

    //MARK:- ----- View lifecycle -----
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    //MARK:- ----- Button Actions ------
    @IBAction func doneButtonTapped(_ sender: Any) {
        self.delegate?.popController(self)
        /*print("done tapped")
        let empVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeViewController") as! EmployeeViewController
        self.navigationController?.pushViewController(empVC, animated: true)*/
    }
}

如果您使用 segue 进行SecondViewController ThirdCiewControler,请参阅unwind segue:过渡到 UITabBarController

并在从ThirdViewController unwind segueSecondViewController后,调用委托方法将数据从SecondViewController传递到FirstViewController

最新更新