简体   繁体   English

关闭没有 NavigationController 的推送视图 controller

[英]Dismissing Pushed view controller without NavigationController

I have two view controllers.我有两个视图控制器。

VC1 - Displays data in a tableView, selecting one of the cells goes to VC2. VC1 - 在 tableView 中显示数据,选择其中一个单元格转到 VC2。 VC2 - Show text fields to edit the data. VC2 - 显示文本字段以编辑数据。

Question - After updating the data and going back to VC1, it does not show the updated data in the table.问题 - 更新数据并返回 VC1 后,它没有在表中显示更新的数据。

I did try adding tableView.reloadData() in ViewWIllAppear but the ViewWillAppear method is not called when I dismiss VC2.我确实尝试在 ViewWIllAppear 中添加 tableView.reloadData() 但是当我关闭 VC2 时不会调用 ViewWillAppear 方法。

//CODE BELOW VC2 - //代码低于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 - VC1 -

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

You have to update the item in the collection of your data that selected from table view您必须更新从表视图中选择的数据集合中的项目

Example:例子:

// 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()

UPDATED更新

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM