我的应用程序里没有故事板。所以我在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!
...
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。