ios - 如何使用 swift 中的委托(delegate)将第二个 View Controller 文本字段值添加到第三个 View Controller 按钮中的第一个 View Controller 标签

标签 ios swift uitableview uibutton

我有三个 View Controller 。在这里,我想从第三个 View Controller 完成按钮将第二个 View Controller 文本字段值添加到第一个 View Controller TableView 标签。 我可以在 popviewcontroller 时使用委托(delegate)将第二个 View Controller 文本字段添加到第一个 View Controller 标签,但我无法从第三个 View Controller 按钮添加。 请帮助我编写代码。

这是我的代码:

这是我的第一个 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完成按钮发送。

最佳答案

第一个 View Controller

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

第二 View Controller

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

第三 View Controller

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

如果您对 SecondViewControllerThirdCiewControler 使用 segue,请参阅 unwind segue:Transition to UITabBarController

在从 ThirdViewControllerSecondViewController unwind segue 后,调用委托(delegate)方法将数据从 SecondViewController 传递到 FirstViewController.

关于ios - 如何使用 swift 中的委托(delegate)将第二个 View Controller 文本字段值添加到第三个 View Controller 按钮中的第一个 View Controller 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56347007/

相关文章:

ios - 如何从 iOS 应用程序中删除这些损坏的选择器

swift - swift Playground 中的简单 swift 委托(delegate)

Swift - 从单元格内的 Button Segue

ios - swift 。为什么 UITableView 委托(delegate)不起作用?

ios - 如何裁剪图像以具有弯曲的不规则边缘?

ios - UICollectionView 在滚动嵌套的 UITableView 后重置内容偏移量

ios - UITableViewCell 为不同的方向和单元格类型加载自定义 XIB

ios - Swift - 选择自定义单元格时如何从堆栈 View 中删除白色背景?

ios - 安装 .der 文件并在完成后返回到应用程序

ios - 如何更改 UICollectionViewCell 高度?