在 swinject 环境下卡住情节提要控制
Posted
技术标签:
【中文标题】在 swinject 环境下卡住情节提要控制【英文标题】:Stuck with storyboards control under swinject environment 【发布时间】:2020-12-09 06:34:57 【问题描述】:您好,我正在尝试为我的 ios 项目应用 swinject,目前我被卡住了..
我的申请中有 2 个部分
-
登录部分
主要部分
场景是如果它在应用程序开始时已经登录 或登录成功后,应将其移至 MAIN Part 我希望他们每个人都有自己的故事板,swinject 范围分别管理实例。
登录完成后,应该新生成 MAIN 部分注入的实例,并且 注销完成后,它应该回到 LOGIN 部分,并且主要部分注入的实例必须消失..
以下是我的视图控制器结构:
使用此名称,我正在尝试使用以下代码导航到 MAIN 视图,但不断出现错误。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else
return
// add these lines
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// if user is logged in before
if let loggedUsername = UserDefaults.standard.string(forKey: "username")
// instantiate the main tab bar controller and set it as root view controller
// using the storyboard identifier we set earlier
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBarController = storyboard.instantiateViewController(identifier: "MainTabBarController")
window?.rootViewController = mainTabBarController
else
// if user isn't logged in
// instantiate the navigation controller and set it as root view controller
// using the storyboard identifier we set earlier
let loginNavController = storyboard.instantiateViewController(identifier: "LoginNavigationController")
window?.rootViewController = loginNavController
extension SwinjectStoryboard
@objc class func setup()
defaultContainer.autoregister(UserProvider.self, initializer: UserProvider.init).inObjectScope(.container)
defaultContainer.storyboardInitCompleted(HomeViewController.self) resolver, controller in
controller.userProvider = resolver ~> UserProvider.self
Storyboard (<SwinjectStoryboard.SwinjectStoryboard: 0x600002304300>) doesn't contain a view controller with identifier 'MainTabBarController'
我应该怎么做才能解决这个问题??
【问题讨论】:
【参考方案1】:AppDelegate 中
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
self.window = UIWindow(frame: UIScreen.main.bounds)
return
HomeVC 中的 SceneDelegate 是您的主要部分,LoginVC 是您的登录部分,例如我的代码
登录成功比 USERDEFAULT.set(true, forKey: "isLogin") 状态 1
var window: UIWindow?
var navigationC: UINavigationController?
private(set) static var shared: SceneDelegate?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else return
if USERDEFAULT.value(forKey:"isLogin") != nil
let homevc = MainStoryboard.instantiateViewController(withIdentifier: "ContainerViewController") as! ContainerViewController
self.navigationC = UINavigationController(rootViewController: homevc)
else
let loginvc = MainStoryboard.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
self.navigationC = UINavigationController(rootViewController: loginvc)
self.navigationC!.setNavigationBarHidden(true, animated: false)
self.window?.clipsToBounds = true
self.window?.rootViewController = navigationC
self.window?.makeKeyAndVisible()
Self.shared = self
【讨论】:
MainStoryboard 是从哪里来的? @yongwoolee 与 let storyboard = UIStoryboard(name: "Main", bundle: nil) 一样,它是声明中的常量文件,因为它对所有 swift 文件都很常见。以上是关于在 swinject 环境下卡住情节提要控制的主要内容,如果未能解决你的问题,请参考以下文章
如何在不使用情节提要的情况下获取视图控制器的 NibName