Reputation: 167
lets say I have MainVC1 and MainVC2 and depending on user type I decide to Load one of them on app start, there isn't much to do to find out which to choose , but I guess its not right to put this on AppDelegate , so I created a VC name PreMain witch decides this , but the problem is that AppStartUp is Ugly cause a VC comes and disappear fast (thePreMain) and then of of the Mains will start , is there any better way to do this ? or is it possible to have an ViewController without a actual View ?
Upvotes: 0
Views: 97
Reputation: 31665
but I guess its not right to put this on AppDelegate
actually that's not right! if you tried to make a workaround as instantiating a pre View Controller -as you mentioned-, that would be the "not right" case.
As mentioned in the application(_:didFinishLaunchingWithOptions:) documentation:
Tells the delegate that the launch process is almost done and the app is almost ready to run.
which seems to be a good starting point to determine the desired initial View controller. You might want to check: set initial viewcontroller in appdelegate - swift Q&A for achieving it, all you have to do is to implement the logic of choosing the desired view controller.
Upvotes: 1
Reputation: 68
but I guess its not right to put this on AppDelegate
It is nothing wrong to choose AppDelegate to do that logic, after all at the didFinishLaunching, you are free to do whatever.
And like you said, the PreMain will briefly show up.
Here's the link that can help you Programmatically set the initial view controller using Storyboards
To avoid any "VC comes and disappear fast"
Upvotes: 1
Reputation: 15778
Beset way is changing the rootViewController in AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
var initialViewController: UIViewController!
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if somecondition {
initialViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController")//replace your FirstViewController identifier
} else {
initialViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController")//replace your SecondViewController identifier
}
window?.rootViewController = initialViewController
window?.makeKeyAndVisible()
return true
}
Upvotes: 1