【问题标题】:Segue saves data on back bar buttonSegue 在返回栏按钮上保存数据
【发布时间】:2017-04-07 17:52:58
【问题描述】:

我在 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 方法

准备继续

    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
    }
}

【问题讨论】:

  • 什么意思?当您在新视图控制器中更改所选笔记时,它会更改先前视图控制器中单元格中的数据,但您不希望更改单元格中的信息,对吗?
  • 保存日期或数据?
  • 你在保存时做什么?
  • @ArtemNovichko 我不想在后栏按钮上更改它,但我有一个保存按钮。问题已更新。

标签: ios iphone uitableview uinavigationcontroller


【解决方案1】:

通过实现 NSObject copy() 方法解决了问题。并将副本发送到 NoteViewController。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2013-03-19
    • 2012-04-10
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多