Segue from LaunchScreen to a viewController in Main.Storyboard?

只愿长相守 提交于 2019-12-04 13:48:16

I got some logic I want to do in my LaunchScreen

Now that I understand the question, I can answer it: don't. Do your time-consuming logic later. Your job is to launch fast. You need to get out of applicationDidFinishLaunchingWithOptions, get out of viewDidLoad, and launch.

What you show at that point is up to you; if you have time-consuming stuff (in your case, it sounds like you're networking or doing something else that takes time while you load up the data source for a table) and you want to show a special view controller that covers the time with a spinning activity view or something, fine. But during actual launch is not the time to do that.

Your LaunchScreen is shown while your app is loading. Go to your AppDelgate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    window.rootViewController = //your root view controller that you have figured out with logic

    return true
}
Dravidian

No you cant code for launchScreen.Storyboard, The reason why :- when your launchScreen.storyboard shows the app is still loading.

Simply put: You cant access your app when it is displaying launchScreen.storyboard, all you can do is make a UI/UX for that not execute any code for it.

Alternative:- Make a viewController that appears as a first viewController check your logic there and do things from there accordingly!

Reference : - https://stackoverflow.com/a/27642160/6297658

Run your check in didFinishLaunchingWithOptions() and use that to "jump" directly to a specific vc. Here's an example using userDefaults, but of course you can replace that with whatever check you're running.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Do some logic

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let welcomeVC = storyboard.instantiateViewControllerWithIdentifier("WelcomeNavController")
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.window?.rootViewController = welcomeVC
            self.window?.makeKeyAndVisible()
        }
}

Add this function into the AppDelegate:

func initialVC(storyboardID: String) {

    let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("\(storyboardID)") as UIViewController
    self.window?.makeKeyAndVisible()

    if storyboardID == "tabBarVC" {

        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.rootViewController = initialViewController

    } else {

        let navController = UINavigationController(rootViewController: initialViewController)
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.rootViewController = navController

    } 
}

In the didFinishLaunchingWithOptions method inside of the AppDelegate, you can add this:

if currentUser != nil {

    initialVC("tabBarVC")

} else {

    initialVC("loginVC")

}

You can see in my example, I am either loading the main app VC or the Login VC depending on if the user is logged in. In your case, You can use an if - else statement and do the logic within the initialVC function.

Note: When I call for the loginVC to be loaded, I have to load the navigationController because the loginVC is embedded in a navigationController. For the tabBarVC, I don't embed the navController because it isn't needed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!