Skip to content

Instantly share code, notes, and snippets.

@likil33
Created September 16, 2022 10:57
Show Gist options
  • Select an option

  • Save likil33/92039fd22f6c96d2e382e947dd8f8285 to your computer and use it in GitHub Desktop.

Select an option

Save likil33/92039fd22f6c96d2e382e947dd8f8285 to your computer and use it in GitHub Desktop.
Root View controller swift
import Foundation
import UIKit
// MARK: Switcher class
enum RootVC:String {
case LoginViewController, TabBarVC
}
class Switcher {
static let shared = Switcher()
private init() {}
static func updateRootVC(_ rootVC:RootVC) {
var navigationController = UINavigationController()
if UserDefaults.standard.bool(forKey: "KLogin") == false {
guard let rootVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController else {
return
}
navigationController = UINavigationController(rootViewController: rootVC)
} else if rootVC == .TabBarVC {
guard let rootVC = UIStoryboard.init(name: "TabBar", bundle: nil).instantiateViewController(withIdentifier: "TabBarVC") as? TabBarVC else {
return
}
navigationController = UINavigationController(rootViewController: rootVC)
}
UIApplication.shared.windows.first?.rootViewController = navigationController
UIApplication.shared.windows.first?.makeKeyAndVisible()
}
func resetRoot() {
var navigationController = UINavigationController()
if UserDefaults.standard.bool(forKey: "KLogin") == true {
guard let rootVC = UIStoryboard.init(name: "TabBar", bundle: nil).instantiateViewController(withIdentifier: "TabBarVC") as? TabBarVC else {
return
}
navigationController = UINavigationController(rootViewController: rootVC)
}else{
guard let rootVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController else {
return
}
navigationController = UINavigationController(rootViewController: rootVC)
}
UIApplication.shared.windows.first?.rootViewController = navigationController
UIApplication.shared.windows.first?.makeKeyAndVisible()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment