首页 > 解决方案 > 关于在 ViewControllers 之间传递数据的问题

问题描述

我有两个视图控制器MainViewControllerDetailViewController.

我将变量从这样传递MainViewControllerDetailViewController ->

class MainController: UIViewController {
...........
func showMatchDetailsView(matchId: Int) {
        let storyboard = UIStoryboard(name: "MatchDetails", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "MatchDetailsIdentifier") as! MatchDetailsController
        controller.matchId = matchId // Is it a good practice?
        controller.modalPresentationStyle = .fullScreen
        navigationController?.pushViewController(controller, animated: true)
        }
}

匹配详细信息控制器

class MatchDetailsController: UIViewController {
    //MARK: Class variable
    var matchId: Int?
     .....
    }

标签: iosswiftuiviewcontroller

解决方案


您可以关注这个项目,其中显示带有数据列表的屏幕(UIViewController),并且所选项目的详细信息显示在另一个屏幕(UIViewController)中。我在使用 MVVM、协调器和容器 DI 模式的 Clean Architecture 之后做了那个示例

Github 中的项目链接:https ://github.com/JLPenaLopez/MyFiles

这是应用程序的一个示例:

示例应用

您也可以关注这篇文章,其中非常清楚地解释了 Clean Architecture、MVVM 和其他模式,以遵循使用 Swift 在 iOS 上的良好实践


推荐阅读