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
2 Answers
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:
4 Comments
hgcahez
Yes, but he return "NSExeption, no controller with the identifier "MyId" "
Dharmesh Kheni
yes because you are giving incorrect identifier to storyboard.
hgcahez
it is in the field need to StoryboardId ID? then check the checkbox below?
hgcahez
Don't check the checkbox ?
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() 
didFinishLaunchingWithOptionsmethod inAppDelegate