I'm trying to automatically switch from lauch screen to home screen after 3 sec. This is code in entry point view controller
override func viewDidAppear(_ animated: Bool) {
DispatchQueue.main.asyncAfter(deadline: .now() .seconds(3)) {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = enter code heremainStoryboard.instantiateViewController(withIdentifier: "GenresNC")
UIApplication.shared.windows.first?.rootViewController = screen
self.view.window?.makeKeyAndVisible()
}
}
It brakes at line with mainStoryboard... Thread 1: EXC_BAD_ACCESS (code=2, address=0x16f5c7de8)
Can somebody help ?
CodePudding user response:
AppDelegate will automatically change from LaunchScreen to window rootViewController - just place your code inside application(application: didFinishLaunchingWithOptions) method:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = mainStoryboard.instantiateViewController(withIdentifier: "GenresNC")
window?.makeKeyAndVisible()
return true;
}
CodePudding user response:
I've implement the same thing in SceneDelegate.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
guard let windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
//self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC: UIViewController?
if AppManager.shared.isFirstLaunch {
rootVC = storyboard.instantiateViewController(identifier: "onBoardViewController") as? OnBoardViewController
AppManager.shared.isFirstLaunch = true
}else{
rootVC = storyboard.instantiateViewController(identifier: "tapBarViewController")
}
let rootNC = UINavigationController(rootViewController: rootVC!)
self.window?.rootViewController = rootNC
self.window?.makeKeyAndVisible()
}