繁体   English   中英

协调器模式没有推送到 NextViewController

[英]coordinator pattern is not pushing to the NextViewController

这是 AppDelegate.swift 实现/配置:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var coordinator: MainCoordinator?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    //        Fabric.with([Crashlytics.self])
    //        FirebaseApp.configure()
    let navigationController = UINavigationController()
    coordinator = MainCoordinator(navigationController: navigationController)
    coordinator?.start()
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

这是 MainCoordinator.swift

import Foundation
import UIKit

class MainCoordinator: Coordinator {
    var subCoordinators = [Coordinator]()
    var navigationController: UINavigationController

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }
    func start() {
        let VC = SplashViewController.instantiate()
        VC.coordinator = self
        navigationController.pushViewController(VC, animated: false)
        NSLog("view Started")


    }

    func goToMerchandizeEntranceView() {

        let vc = MerchandizeEntranceViewController.instantiate()
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    }

    func gotoMerchandizeRequestView() {

        let vc = MerchandizeRequestStatusViewController.instantiate()
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    }


}

在 MainVC 内部,在收集视图的 didselectItemAt IndexPath 方法中应该触发到下一个 VC,但是,VC 没有被触发并显示在屏幕上,但是“NsLog”打印消息..任何人都可以帮我解释我错在哪里..?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if collectionView == menuCollectionView {
        switch indexPath.row {
        case 0:
            coordinator?.goToMerchandizeEntranceView()
            NSLog("Receiving touch")
        case 1:
            coordinator?.gotoMerchandizeRequestView()
             NSLog("Receiving touch")
        default:
            break
        }
    }
    else {

        NSLog("define action for story cell")
    }

}

更新:由于您使用的是 storyboard,因此您应该像这样从 storyboard 实例化视图 controller:

func goToMerchandizeEntranceView() {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if let vc = storyboard.instantiateViewController(withIdentifier: "MerchandizeEntranceViewController") as? MerchandizeEntranceViewController {
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    } else {
        print("Could not instantiateViewController: MerchandizeEntranceViewController")
    }
}

func gotoMerchandizeRequestView() {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if let vc = storyboard.instantiateViewController(withIdentifier: "MerchandizeRequestStatusViewController") as? MerchandizeRequestStatusViewController {
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    } else {
        print("Could not instantiateViewController: MerchandizeEntranceViewController")
    }
}

似乎您的协调器未在MainVC中初始化。 您应该在viewDidLoad中初始化您的协调器:

override func viewDidLoad() {
    super.viewDidLoad()
    if let navigationController = navigationController {
        coordinator = MainCoordinator(navigationController: navigationController)
    }
}

我想,除了上面提到的视图控制器实例化之外,另一点可能是问题的根源是在将导航 controller 添加到 window 之前调用coordinator.start 。我会将 AppDelegate 修改为如下所示:

let navigationController = UINavigationController()
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()

coordinator = MainCoordinator(navigationController: navigationController)
coordinator?.start()

这是 AppDelegate.swift 实现/配置:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var coordinator: MainCoordinator?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    //        Fabric.with([Crashlytics.self])
    //        FirebaseApp.configure()
    let navigationController = UINavigationController()
    coordinator = MainCoordinator(navigationController: navigationController)
    coordinator?.start()
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

这是 MainCoordinator.swift

import Foundation
import UIKit

class MainCoordinator: Coordinator {
    var subCoordinators = [Coordinator]()
    var navigationController: UINavigationController

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }
    func start() {
        let VC = SplashViewController.instantiate()
        VC.coordinator = self
        navigationController.pushViewController(VC, animated: false)
        NSLog("view Started")


    }

    func goToMerchandizeEntranceView() {

        let vc = MerchandizeEntranceViewController.instantiate()
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    }

    func gotoMerchandizeRequestView() {

        let vc = MerchandizeRequestStatusViewController.instantiate()
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    }


}

在 MainVC 内部,在集合 View 的 didselectItemAt IndexPath 方法中应该触发到下一个 VC,但是,VC 没有被触发并显示在屏幕上,“NsLog”虽然打印了消息..谁能帮我解释我错在哪里..?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if collectionView == menuCollectionView {
        switch indexPath.row {
        case 0:
            coordinator?.goToMerchandizeEntranceView()
            NSLog("Receiving touch")
        case 1:
            coordinator?.gotoMerchandizeRequestView()
             NSLog("Receiving touch")
        default:
            break
        }
    }
    else {

        NSLog("define action for story cell")
    }

}

暂无
暂无

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

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