In my first view controller, I have two instances of a class.
let test = MFDScreenModel()
let testtwo = MFDScreenModel()
they have a method to change variables
test.setDataVars(title: "", DP: "")
there are two buttons linked to one IBaction
when either button is touched, a segue to a table view is called.
@IBAction func sendData(_ sender: UIButton) {
print(sender.currentTitle as Any)
self.performSegue(withIdentifier: "goToSettings", sender: self)
The table view:
class GuageSettingsViewController: UIViewController{
@IBOutlet weak var tableView: UITableView!
var dataPointList = MenuInfo()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.dataSource = self
tableView.delegate = self
}
}
extension GuageSettingsViewController: UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.dismiss(animated: true, completion: nil)
//Need to return this data with the Identifying info from the previous segue
}
}
extension GuageSettingsViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataPointList.menuList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath)
cell.textLabel?.text = dataPointList.menuList[indexPath.row].title
return cell
}
}
I need the returned data from the table view in a way that it will only change the vars passed
test.setDataVars(title: "", DP: "")
on either instance based on which button is pressed.
CodePudding user response:
In GuageSettingsViewController
create a callback closure.
I don't know which type menuList
represents, replace MenuList
with the actual type
var callback : ((MenuList) -> Void)?
In the first view controller override prepare(for segue
and assign the callback
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "goToSettings" else { return }
let destination = segue.destination as! GuageSettingsViewController
destination.callback = { [unowned self] info in
// do something with info
}
}
In GuageSettingsViewController
/ didSelectRowAt
insert this line
callback?(dataPointList.menuList[indexPath.row])