开发者社区> 问答> 正文

如何解决依赖swinject(没有故事板),另一个重要的控制器作为rootVC在哪里?

我的应用程序里没有故事板。所以我在AppDelegate中详细说明了我的所有依赖项。我阅读了Swinject的文档,并在这里建议了以下选项:

window.rootViewController=收集器。解析(PersonViewController.Self)

但是在我的例子中,根控制器是选项卡条控制器(MainTabBarController()),它设置“我的集合”视图和其他选项的布局。怎么才能解决这个问题呢?

起初,我试图通过Swinject Storyboard来完成所有事情,但是故事板InitCompleted()对我和我的viewModel: ViewModelProtocol!留着nil(控制台解析失败)。

应用程序代表:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  let container: Container = {
    let container = Container()
    container.register(UnsplashService.self){ _ in UnsplashService()}//.inObjectScope(.container)
    container.register(UnsplashManagerProtocol.self){ r in UnsplashDataFetcher(with: r.resolve(UnsplashService.self)!)}//.inObjectScope(.container)
    container.register(ViewModelProtocol.self){ r in ViewModel(withClient: r.resolve(UnsplashManagerProtocol.self)!)}//.inObjectScope(.container)
    container.register(PhotosCollectionViewController.self) { r in
      let controller = PhotosCollectionViewController()
      controller.viewModel = r.resolve(ViewModelProtocol.self)
      return controller
    }
    return container
  }()

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)
    let homeVC = UIViewController()
    homeVC.view.backgroundColor = .white
    window!.rootViewController = MainTabBarController()
    window!.makeKeyAndVisible()
    //window.rootViewController = container.resolve(PhotosCollectionViewController.self)

    return true
  }

MainTabBarController:

class MainTabBarController: UITabBarController {

  var photosVC: PhotosCollectionViewController!
  var likesVC: LikesCollectionViewController!

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

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    photosVC = PhotosCollectionViewController(collectionViewLayout: WaterfallLayout())

    likesVC = LikesCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())

    viewControllers = [
      generateNavigationController(rootViewController: photosVC, title: "Photos", image: #imageLiteral(resourceName: "photos")),
      generateNavigationController(rootViewController: likesVC, title: "Favourites", image: #imageLiteral(resourceName: "heart"))
    ]
  }

  private func generateNavigationController(rootViewController: UIViewController, title: String, image: UIImage) -> UIViewController {
    let navigationVC = UINavigationController(rootViewController: rootViewController)
    navigationVC.tabBarItem.title = title
    navigationVC.tabBarItem.image = image
    return navigationVC
  }
}

照片收集视图控制器:

class PhotosCollectionViewController: UICollectionViewController {

  var viewModel: ViewModelProtocol!

  ...
}

展开
收起
游客5akardh5cojhg 2019-12-11 22:24:10 569 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Mock平台-让测试插上翅膀 立即下载
动态、高效,蚂蚁动态卡片的内核逻辑 立即下载
内容驱动游戏分发 立即下载