我有三个视图控制器。在这里,我想从第三个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完成按钮发送。
答案 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)*/
}
}
如果您使用segue
到SecondViewController
到ThirdCiewControler
,请参考unwind segue
:Transition to UITabBarController
在unwind segue
从ThirdViewController
到SecondViewController
之后,调用委托方法将数据从SecondViewController
传递到FirstViewController
。