Skip to content

Instantly share code, notes, and snippets.

@Myrrel
Last active March 11, 2024 20:16
Show Gist options
  • Save Myrrel/12b944e6b23dd44abaf70b152d614f62 to your computer and use it in GitHub Desktop.
Save Myrrel/12b944e6b23dd44abaf70b152d614f62 to your computer and use it in GitHub Desktop.
UIKit + Coordinator
import Foundation
import UIKit
protocol Coordinator {
var viewController: UIViewController? {get}
var navigationController: UINavigationController? {get}
func start()
}
extension Coordinator {
var viewController: UIViewController? {nil}
var navigationController: UINavigationController? {nil}
}
import Foundation
import UIKit
class MainCoordinator: Coordinator {
var navigationController: UINavigationController?
init(navigationController: UINavigationController? = nil) {
self.navigationController = navigationController
}
func start() {
let vc = ViewController()
navigationController?.pushViewController(vc, animated: true)
}
func showDetail(item: Model) {
let vc = DetailViewController(item: item)
navigationController?.pushViewController(vc, animated: true)
}
}
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
var mainCoordinator: MainCoordinator?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let navigation = UINavigationController()
mainCoordinator = MainCoordinator(navigationController: navigation)
window?.rootViewController = navigation
window?.makeKeyAndVisible()
mainCoordinator?.start()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment