如何使用Swift中的委托将TableView单元格文本字段调用为按钮动作

时间:2019-05-19 09:25:01

标签: swift uitableview uibutton delegates

我在两个视图控制器中有两个表视图。现在我想在单击EmployeeViewController标签的按钮时发送EmployeeProfileViewController文本字段数据。为此,我在EmployeeProfileViewController中创建了EmployeeDelegateProtocol。但是在这里,我无法将tableview textfield.text添加到委托方法。

发生错误:使用无法解析的标识符“ firstName”

这是我的代码:

import UIKit

protocol EmployeeDelegateProtocol {
func sendDataToEmployeeViewController(myData: String)
}

class EmployeeProfileViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate,UIImagePickerControllerDelegate {

var delegate: EmployeeDelegateProtocol?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCell(withIdentifier: "cell4", for: indexPath) as? EmployeeProfileTableViewCell
        cell?.saveButtonTapped.addTarget(self, action: #selector(saveButtonClicked), for: .touchUpInside)

    return cell
}

@objc func saveButtonClicked(){
    print("button clicked")
    self.delegate?.sendDataToEmployeeViewController(myData: firstName.text!)
}
}

在EmployeeViewController代码中:

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
    //Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return namesArray.count
}

func sendDataToEmployeeViewController(myData: String) {
    self.namesArray.append(myData)
    tableView.reloadData()
    print(namesArray)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EmployeeTableViewCell
    cell.empName.text = namesArray[indexPath.row]

    cell.viewProfileButton.addTarget(self, action: #selector(viewprofileBUttonClicked), for: .touchUpInside)
    cell.removeRowButton.addTarget(self, action: #selector(removerowButtonClicked), for: .touchUpInside)

    return cell
}


@objc func viewprofileBUttonClicked(sender : UIButton!){
    print("view profile clicked")
    let profileVC = self.storyboard?.instantiateViewController(withIdentifier: "EmployeeProfileViewController") as! EmployeeProfileViewController
    self.navigationController?.pushViewController(profileVC, animated: true)
}

@objc func removerowButtonClicked(sender : UIButton!) {
    namesArray.remove(at: sender.tag)
    self.tableView.reloadData()
}

@IBAction func addButtonClicked(_ sender: Any) {

    var empProfileVC = self.storyboard!.instantiateViewController(withIdentifier: "EmployeeProfileViewController") as! EmployeeProfileViewController
    empProfileVC.delegate = self

    self.navigationController?.pushViewController(empProfileVC, animated: true)
}
}

请帮助我解决错误。预先感谢。

0 个答案:

没有答案