I am looking how to pass data from one child View controller to the parent view controller. They both contain tableViews. I want to select the cell in View Controller B and hit a button and send that data back to View controller A. I am using Delegates and Protocol. My issue is that i am not sure what to use in the IBAction for the button to make it appear in the parent view controller.
My View Controller A:
class ViewController: UIViewController, DataEnteredDelegate, UITableViewDelegate, UITableViewDataSource, DataSelectedDelegate {
@IBOutlet weak var tableView: UITableView!
var tableArrayHeader = [String]()
var tableViewArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecondViewController", let secondViewController = segue.destination as? SecondViewController {
secondViewController.delegate = self
} else {
if segue.identifier == "showThirdViewController", let thirdViewController = segue.destination as? ThirdViewController {
thirdViewController.selectedDelegate = self
}
}
}
// required method of our custom DataEnteredDelegate protocol
func userDidEnterInformation(info: String) {
navigationController?.popViewController(animated: true)
self.tableArrayHeader.append(info)
self.tableView.reloadData()
}
func userDidSelectInformation(selection: String) {
navigationController?.popViewController(animated: true)
self.tableArrayHeader.append(selection)
self.tableView.reloadData()
}
@IBAction func addingButton(_ sender: Any) {
tableViewArray.insert("new Cell", at: 0)
tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .right)
}
func numberOfSections(in tableView: UITableView) -> Int {
return tableArrayHeader.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return tableArrayHeader[section]
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as? CustomHeaderTableViewCell
cell?.headerLabel?.text = tableArrayHeader[section]
cell?.backgroundColor = UIColor.lightGray
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 55
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FirstTableViewCell
cell.label.text = tableViewArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 55
}
// func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// tableView.deselectRow(at: indexPath, animated: true)
// }
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
tableViewArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .bottom)
tableView.endUpdates()
} else{
print("there are no cells to delete")
}
}
}
View Controller B:
protocol DataSelectedDelegate {
func userDidSelectInformation(selection: String)
}
class ThirdViewController: ViewController {
var selectedDelegate: DataSelectedDelegate?
struct Objects {
var sectionName : String!
var sectionObjects : [String]!
}
var objectArray = [Objects]()
var tableArray = ["this","is","test","date"]
var names = ["Vegetables": ["Tomato", "Potato", "Lettuce"], "Fruits": ["Apple", "Banana"]]
//var objectArray = [String]()
var selectionArray : [String] = []
var selectedIndexPath = [IndexPath]()
var selectedIndex = -1
@IBOutlet weak var thirdTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
for (key, value) in names {
print("(key) -> (value)")
objectArray.append(Objects(sectionName: key, sectionObjects: value))
}
}
@IBAction func sendDataFromTableToTable(_ sender: Any) {
selectedDelegate?.userDidSelectInformation(selection: // This is where i am unsure of what to put so that the selected cell data will go back when the button is pressed "" )
}
// @IBAction func sendDataFromRowToTable(_ sender: Any) {
// selectedDelegate?.userDidSelectInformation(selection: selectedIndexPath.description ?? "" )
// }
override func numberOfSections(in tableView: UITableView) -> Int {
return objectArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectArray[section].sectionObjects.count
//return tableArray.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectArray[section].sectionName
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 55
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "thirdCell", for: indexPath)
cell.textLabel?.text = objectArray[indexPath.section].sectionObjects[indexPath.row]
cell.textLabel?.text = tableArray[indexPath.row]
if selectedIndexPath.contains(indexPath) {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedIndexPath.contains(indexPath) {
selectedIndexPath.removeAll() {
(selectedIndexPath) -> Bool in
selectedIndexPath == indexPath
}
thirdTable.reloadRows(at: [indexPath], with: .fade)
print(selectedIndexPath.description)
} else {
selectedIndexPath.append(indexPath)
thirdTable.reloadRows(at: [indexPath], with: .fade)
}
selectionArray.append(tableArray[indexPath.row])
print(selectionArray)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let index = selectionArray.firstIndex(of: tableArray[indexPath.row]) {
selectionArray.remove(at: index)
print(tableArray)
}
}
}
Images of the controllers:
question from:
https://stackoverflow.com/questions/66052032/passing-data-from-view-controller-b-using-a-button-to-view-contoller-a-with-tabl 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…