在没有 NavigationController 的情况下关闭推送视图控制器

Dismissing Pushed view controller without NavigationController

我有两个视图控制器。

VC1 - 在 tableView 中显示数据,选择其中一个单元格转到 VC2。 VC2 - 显示文本字段以编辑数据。

问题 - 更新数据并返回到 VC1 后,table 中不显示更新的数据。

我确实尝试在 ViewWIllAppear 中添加 tableView.reloadData(),但是当我关闭 VC2 时,不会调用 ViewWillAppear 方法。

//代码如下 VC2-

@IBAction func saveTask(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)
    view.endEditing(true)
    if let task = task {
        task.completed = toggleStatus.isOn
    }
}

VC1 -

    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    tableView.reloadData()
}

您必须更新从 table 视图

中选择的数据集合中的项目

示例:

// The collection of your data is used to show in table view
var data: [String] = []

// After navigated back to the VC1, you have to update like:
data[you_selected_index] = inputData // From VC2
tableview.reloadData()

已更新

class VC1: UIViewController {
    private var selectedIndex: Int?

}
extension VC1: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedIndex = indexPath.row
        let vc = VC2()
        vc.delegate = self
        present(vc, animated: true, completion: nil)
    }
}
// MARK: - InputInfoVCDelegate
extension VC1: VC2Delegate {

    func onInputInfoSuccessUpdated(source: String?) {
        // Updating data here
        guard let index = selectedIndex else { return }
        data[index] = source
        tableView.reloadData()
    }
}
protocol VC2Delegate: class {
    func onInputInfoSuccessUpdated(source: String?)
}
class VC2: UIViewController {

    weak var delegate: VC2Delegate?

    @IBAction private func actionTapToBackButton(_ sender: Any) {
        delegate?.onInputInfoSuccessUpdated(source: inputTextField.text)
    }
}