I want to use Coordinator pattern in Model View Presenter architecture. I don't know how to send data back properly.
I have a main view controller with table view and button which, when clicked, present view controller with categories to select. When user selects category I want to dismiss view controller and filter data with selected category.
How to send selected category to main view controller when dismissed view controller?
protocol MainViewProtocol: class {
func reloadTableView()
}
class MainViewController {
var presenter: MainPresenterProtocol?
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var categoryButton: UIButton!
@IBAction func selectCategory(_ sender: UIButton) {
presenter?.selectCategory()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return presenter!.currentCount
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ExerciseTableViewCell = tableView.dequeueResuableCell(for: indexPath)
guard let exercise = presenter?.exercises[indexPath.row] else { return UITableViewCell() }
cell.setCell(exerciseName: exercise.name)
return cell
}
.. other table view initializing functions
}
extension MainViewController: MainViewProtocol {
func reloadTableView() {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
protocol MainPresenterProtocol: class {
var exercises: [Exercise] { get set }
func selectCategory()
func setSelectedCategory(category: String)
}
class MainPresenter: MainPresenterProtocol {
var exercises = [Exercise]() {
didSet {
view?.reloadTableView()
}
}
weak var coordinatorDelegate: MainCoordinatorDelegate?
weak var view: MainViewProtocol?
init(view: MainViewProtocol?, coordinatorDelegate: MainCoordinatorDelegate?) {
self.view = view
self.coordinatorDelegate = coordinatorDelegate
}
func selectCategory() {
coordinatorDelegate.selectCategory()
}
func setSelectedCategory(category: String) {
// Here I filter exercises for selected category
}
... other functions
}
protocol MainCoordinatorDelegate: class {
func selectCategory()
}
class MainCoordinator: MainCoordinatorDelegate {
...initalize functions
func start() {
//HERE I CREATE Main View Controller
let mainVC = AppStoryboard.Main.instantiate(MainViewController.self)
let presenter = MainPresenter(view: mainVC, coordinatorDelegate: self)
mainVC.presenter = presenter
navigationController.pushViewController(mainVC)
}
func selectCategory() {
let categoryVC = AppStoryboard.Main.instantiate(CategoryViewController.self)
categoryVC.coordinatorDelegate = self
navigationController.present(categoryVC)
}
func setSelectedCategory(category: String) {
navigationController.visibleViewController?.dismiss(animated: true, completion: {
guard let vc = self.navigationController.visibleViewController as? MainViewController else { return }
vc.presenter?.setSelectedCategory(category: category)
}
}
class CategoryViewController {
var coordinatorDelegate: MainCoordinatorDelegate?
var selectedCategory = ""
//here I have a functions for table view and set selected section value in selectedCategory
@IBAction func saveCategory(_ sender: UIButton) {
coordinatorDelegate?.setSelectedCategory(category: selectedCategory)
}
}
How to send data back and set it in View Controller using coordinator pattern? Using navigation controller to check what is current visible view controller and using it to set data back is it a correctly way to do this?
The usual solution is the protocol-and-delegate pattern, and this works just fine with Coordinator pattern.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.