如何使用Swift中的委托将第二个viewcontroller文本字段值添加到thirdviewcontroller按钮中的firstviewcontroller标签

时间:2019-05-28 17:01:19

标签: ios swift uitableview uibutton

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

这是我的代码:

这是我的第一个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)
}
}

请建议我如何从Thirdvc完成按钮发送。

1 个答案:

答案 0 :(得分:0)

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

SecondViewController

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

ThirdViewController

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

如果您使用segueSecondViewControllerThirdCiewControler,请参考unwind segueTransition to UITabBarController

unwind segueThirdViewControllerSecondViewController之后,调用委托方法将数据从SecondViewController传递到FirstViewController