Abs Abs - 5 months ago 66
iOS Question

set initial viewcontroller in appdelegate - swift

I would like to set the initial viewcontroller from the appdelegate. I found a really good answer, however it's in Objective C and im having trouble achieving the same thing in swift.

Programatically set the initial view controller using Storyboards

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];

self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];

return YES;
}


Anyone able to help?

I want the initial Viewcontroller to be dependent on certain conditions being met using a conditional statement.

Abs Abs
Answer

I used this thread to help me convert the objective C to swift, and its working perfectly.

Instantiate and Present a viewController in Swift

code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    var storyboard = UIStoryboard(name: "Main", bundle: nil)

    var initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginSignupVC") as! UIViewController

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()

    return true
}