在 swinject 环境下被故事板控制卡住了 [英] Stuck with storyboards control under swinject environment

查看:26
本文介绍了在 swinject 环境下被故事板控制卡住了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的 IOS 项目应用 swinject,目前我被卡住了..

Hi I am trying to apply swinject for my IOS project and currently I have been stuck..

我的申请中有 2 个部分

I have 2 part in my application

  1. 登录部分
  2. 主要部分

场景是如果它在应用程序开始时已经登录或者当登录成功完成后,它应该被移动到 MAIN Part我希望他们每个人都有自己的故事板,由 swinject 作用域单独管理实例.

Scenario is if it's logged in already when application begins or when login is successfully done, it should be moved to MAIN Part and I hope to Each of them has their own storyboard that the swinject scope manages the instances separately.

登录完成后,MAIN部分注入的实例应该是新生成的,并且注销完成后,它应该回到LOGIN部分并且主要部分注入的实例必须消失..

when login is done, the MAIN part injected instance should be newly generated and when logout is done, it should be going back to LOGIN part and the main part injected instance must be gone..

以下是我的视图控制器结构:

The below is my view controllers structure:

使用此名称,我正在尝试使用以下代码导航到 MAIN 视图,但一直出现错误.

with this designation, I am trying to navigate to MAIN views with the codes below but keep getting error.

    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'

我该怎么做才能解决这个问题??

What am I supposed to do for solving this??

推荐答案

AppDelegate in

AppDelegate in

var window: UIWindow?
    
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {    
    self.window = UIWindow(frame: UIScreen.main.bounds)
    return     
}

SceneDelegate 在 HomeVC 你的主要部分和 LoginVC 你的登录部分,例如我的代码

SceneDelegate in HomeVC your Main Part and LoginVC your Login part for example my code

登录成功状态 1 比 USERDEFAULT.set(true, forKey: "isLogin")

Login successfuly staus 1 than USERDEFAULT.set(true, forKey: "isLogin")

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
}

这篇关于在 swinject 环境下被故事板控制卡住了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆