Reputation: 413
I work with Xcode Swift 7 and 2. I want to know how to execute an action before viewDidLoad my initial view. In fact I would like to change the initial view based on a parameter (if I am logged in or not) ... Should we do it in the AppDelegate? Thank you
Upvotes: 0
Views: 1298
Reputation: 751
let mainStorybord : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
Bool isAlreadyLogin = <...code for to check for already logged in or not...>
if isAlreadyLogin
{
var homeVC = storyboard.instantiateViewControllerWithIdentifier("<homeVC>") as! UIViewController
// HERE <homeVC> will be your identifier for your sugue in storyboard for initial view after login.
let nav : UINavigationController = UINavigationController(rootViewController: homeVc)
self.window?.rootViewController = nav
}
else
{
var loginVC = storyboard.instantiateViewControllerWithIdentifier("<loginVC>") as! UIViewController
// HERE <loginVC> will be your identifier for your sugue in storyboard for login view
let nav : NavigationViewController = UINavigationController(rootViewController: loginVC)
self.window?.rootViewController = nav
}
self.window?.makeKeyAndVisible()
Upvotes: 0
Reputation: 71862
This way you can initiate specific viewController from appdelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("yourID")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
Assign your storyboard ID this way:
Click on your storyboard then go to Identity Inspector at right side and give a storyboard id as shown in below image:
Upvotes: 1