문제 설명
Segue는 뒤로 막대 버튼에 데이터를 저장합니다. (Segue saves data on back bar button)
TableViewCell 클릭 시 segue가 있고 다른 ViewController에 저장 버튼이 있습니다. 저장 버튼에서는 제대로 작동하지만 뒤로 바 버튼을 클릭하면 TableViewCell도 변경됩니다. 변경 사항을 어떻게 취소할 수 있습니까? NoteTableViewController와 NoteViewController가 있습니다
TableViewController 메소드
tableView cellRowAt 메소드
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ‑> UITableViewCell {
let cellIdentifier = "NoteTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! NoteTableViewCell
let note = notes[indexPath.row]
cell.noteLabel.text = note.note
return cell
}
TableViewController가 segue 메소드를 준비합니다
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowDetail" {
let noteDetailViewController = segue.destination as! NoteViewController
if let selectedNoteCell = sender as? NoteTableViewCell {
let indexPath = tableView.indexPath(for: selectedNoteCell)!
let selectedNote = notes[indexPath.row]
noteDetailViewController.note = selectedNote
noteDetailViewController.oldNote = someNote
}
}
}
이 메소드는 저장용입니다 데이터, NoteViewController의 saveButton에 연결
@IBAction func unwindToNoteList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.source as? NoteViewController {
let note = sourceViewController.note
if note?.note != nil {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
notes[selectedIndexPath.row] = note!
tableView.reloadRows(at: [selectedIndexPath], with: .none)
} else {
let newIndexPath = NSIndexPath(row: notes.count, section: 0)
notes.append(note!)
tableView.insertRows(at: [newIndexPath as IndexPath], with: .top)
}
saveNotes()
}
}
}
NoteViewController 메소드
segue 준비
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if saveButton === sender as? UIBarButtonItem {
if let noteText = noteTextView.text {
note?.note = noteText
}
note?.toDate = remindDate
note?.image = photoImageView.image
} else {
note = oldNote
}
}
참조 솔루션
방법 1:
Problem solved, by implementation NSObject copy() method. And sending a copy to NoteViewController.
(by Ivan Zolotarov、Ivan Zolotarov)