It's a little bit of a description of what you want, but if I understood correctly, you would basically need to have a protocol:
protocol AddCarProtocol: class {
func didSaveNewCar(car: Car)
}
and a variable weak var delegate: AddCarProtocol?
that your driver would have to add cars and that you would set from the controller that shows the list of cars, in prepareForSegue
if you use segues
.
You could define a structure Car
to contain the information of a car:
struct Car {
var name: String
var checkDetail: String
var checkPoints: [String]
}
So in your driver to add cars, you could have an array with possible checkpoints:
let availableCheckPoints = ["Alineacion", "Balanceo", "Cambio aceite", "Mantenimiento A/C"]
thus your method to save the car:
@IBAction func doSaveCar(_ sender: Any) {
guard let carName = self.tfCar.text, let checkDetail = self.tfDetail.text else {
return
}
let checkPoints = self.getSelectedCheckPoints()
let car = Car(name: carName, checkDetail: checkDetail, checkPoints: checkPoints)
self.delegate?.didSaveNewCar(car: car)
_ = self.navigationController?.popViewController(animated: true)
}
private func getSelectedCheckPoints() -> [String] {
if let selectedIndexPaths = self.tableview.indexPathsForSelectedRows {
var selectedCheckpoints: [String] = []
selectedIndexPaths.forEach({ (indexPath) in
selectedCheckpoints.append(self.availableCheckPoints[indexPath.row])
})
return selectedCheckpoints
}
return []
}
So in your viewController of the car list, you would have the delegated method:
extension ViewController: AddCarProtocol {
func didSaveNewCar(car: Car) {
self.cars.append(car)
self.tableview.reloadData()
}
}
and finally to move to the controller that shows the details of a car:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "showCarDetailsVC", sender: self.cars[indexPath.row])
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showAddCarViewController" {
let vc = segue.destination as? AddCarViewController
vc?.delegate = self
}
else if segue.identifier == "showCarDetailsVC" {
let vc = segue.destination as? CarDetailViewController
vc?.car = sender as? Car
}
}
and you would have available the data of the selected car, to show them in the controller in viewDidLoad
for example.