gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:43:34 24 4
gpt4 key购买 nike

我有三个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com