简体   繁体   English

如何解决在 Swift 中通过 Coordinator 的操作打开 ViewController 的问题?

[英]How to fix problem with opening ViewController by action from Coordinator in Swift?

I'm trying to open another controller by tapping on the cell of my tableView.我正在尝试通过点击 tableView 的单元格来打开另一个 controller。 I'm coding with MVVM and Coordinator pattern.我正在使用 MVVM 和协调器模式进行编码。

In the beginning we see this screen - it is declarated in the method start()一开始我们看到这个屏幕 - 它在方法 start() 中声明

let service = Service()
private(set) weak var navigationController: UINavigationController?

func start() -> UINavigationController {
    let vm = ContinentsViewModel(service: service)
    let vc = ContinentsViewController(viewModel: vm)
    let navigationController = UINavigationController()
    self.navigationController = navigationController
    navigationController.setViewControllers([vc], animated: false)
    bindContinentsViewModel(viewModel: vm)
    return navigationController
}

pic.1 - func start()

Later, my goal is to open all list of countries of the continent, but now l just need to open empty ViewController by tap on the cell (ex. Africa or Antarctica).后来,我的目标是打开该大陆的所有国家/地区列表,但现在我只需要通过点击单元格(例如非洲或南极洲)来打开空的 ViewController。 Here is my methods for it, but they don't work.这是我的方法,但它们不起作用。

private func showCountries() {
    let vc = ViewController()
    navigationController?.pushViewController(vc, animated: true)
}

private func bindContinentsViewModel(viewModel: ContinentsViewModel) {
    viewModel
        .flow
        .bind { [weak self] flow in
            switch flow {
            case .onContinentTap:
                self?.showCountries() // don't work
              //  print("show \(continent)") // work - continent is a param of .onContinentTap, which prints an geo-id of the continent, just if you need to know.
            }
        }
        .disposed(by: viewModel.bag)
}

Thank you so much!太感谢了!

The following works as expected.以下工作按预期进行。 What are you doing differently?你在做什么不同?

@main
final class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    var viewModel: ViewModel?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        viewModel = ViewModel()
        let controller = viewModel?.start()

        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = controller
        window?.makeKeyAndVisible()

        return true
    }
}

final class ViewModel {
    private(set) weak var navigationController: UINavigationController?

    func start() -> UINavigationController {
        let vm = ContinentsViewModel()
        let vc = ContinentsViewController(viewModel: vm)
        let navigationController = UINavigationController()
        self.navigationController = navigationController
        navigationController.setViewControllers([vc], animated: false)
        bindContinentsViewModel(viewModel: vm)
        return navigationController
    }

    private func showCountries() {
        let vc = UIViewController()
        vc.view.backgroundColor = .blue
        navigationController?.pushViewController(vc, animated: true)
    }

    private func bindContinentsViewModel(viewModel: ContinentsViewModel) {
        viewModel.flow
            .bind { [weak self] flow in
                switch flow {
                case .onContinentTap:
                    self?.showCountries()
                }
            }
            .disposed(by: viewModel.bag)
    }
}

final class ContinentsViewModel {
    enum Flow {
        case onContinentTap
    }
    let flow: Observable<Flow>
    let bag = DisposeBag()

    init() {
        flow = .just(.onContinentTap)
            .delay(.seconds(3), scheduler: MainScheduler.instance)
    }
}

final class ContinentsViewController: UIViewController {
    var viewModel: ContinentsViewModel
    init(viewModel: ContinentsViewModel) {
        self.viewModel = viewModel
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
    }
}

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

相关问题 如何在ViewController Swift上修复PageMenu - How to fix PageMenu on ViewController Swift 快速以编程方式打开viewController - Opening viewController programmatically in swift 如何从ViewController访问Swift 3中的另一个viewController - How to access from viewController another viewController in Swift 3 如何通过使用滚动操作从子viewcontroller继承父viewcontroller? - How to come parent viewcontroller from child viewcontroller by using scrolling action? 如何将数据从模态呈现的 ViewController 传递到 swift 5 中的父 ViewController? - How to pass the data from modally presented ViewController to parent ViewController in swift 5? 如何将数据从 Swift ViewController 传递到 Obj-C ViewController? - How to pass data from a Swift ViewController to an Obj-C ViewController? swift 5中如何从viewcontroller发送数据到embedded viewcontroller? - How to send data from viewcontroller to embded viewcontroller in swift 5? 如何快速将图像从一个视图控制器传递到另一个视图控制器? - How to pass an image from one viewcontroller to another viewcontroller in swift? 如何使用Swift 3将数据从ViewController A传递到另一个ViewController B - How to pass data from viewcontroller A to another viewcontroller B using swift 3 Memory Swift 中特定 ViewController 的问题 5 - Memory Problem with specific ViewController in Swift 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM