Вы можете использовать шаблон делегата:
protocol DetailViewDelegate: class {
func detailViewDidUpdate(from: String?, to: String?)
}
class DetailViewController : UIViewController {
var detailItem: String?
weak var delegate: DetailViewDelegate?
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.textField.text = detailItem
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.delegate?.detailViewDidUpdate(from: detailItem, to: textField.text)
}
}
Тогда в вашем MasterViewController :
extension MasterViewController: DetailViewDelegate {
func detailViewDidUpdate(from: String?, to: String?) {
guard let originalValue = from, let newValue = to else {
return
}
if let indexPath = self.objects.index(of: originalValue) {
self.objects[indexPath] = newValue
// We assume here that there is only one section
let newIndexPath = IndexPath(row: indexPath, section: 0)
self.tableView.reloadRows(at: [newIndexPath], with: .automatic)
}
}
}
Не забудьте назначить переменную делегата DetailViewController в вашем MasterController prepare (для segue: UIStoryboardSegue, sender: Any?) method