skip to Main Content

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 ?

2

Answers


  1. 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;
    }
    
    Login or Signup to reply.
  2. 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)
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootVC: UIViewController = storyboard.instantiateViewController(identifier: "tapBarViewController")
    
        let rootNC = UINavigationController(rootViewController: rootVC!)
        self.window?.rootViewController = rootNC
        self.window?.makeKeyAndVisible()
        
    } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search