0
votes

I want to display a modal view controller to the user should there not be a user logged in. Here is my method implementation:

func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool
{
    //  Notifications
    //
    //      User
    //
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "noCurrentUser:", name: UserCurrentUserNotSetNotificationName, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "currentUserDidChange:", name: UserCurrentUserDidChangeNotificationName, object: nil)

    //  Root window
    //
    if managedObjectContext != nil && User.currentUser(managedObjectContext!) == nil
    {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        if let logInViewController = storyboard.instantiateViewControllerWithIdentifier("Log In View Controller") as? LogInViewController
        {
            window?.rootViewController?.presentViewController(logInViewController, animated: false, completion: nil)
        }
    }

    return true
}

I include the notifications because currently, the noCurrentUser: method presents my log in view controller modally and animated. This works well except when the app launches, the user sees a flash of the app (the root view controller) before the the notification is sent and the modal log in view controller is presented.

I’ve tried setting the modal animation option to false on presenting, but because it’s not the root view controller, this still doesn’t work.

So how do I properly set a root view controller to a modal view controller which I can then dismiss modally.

1
I've provided an answer to a similar question in another question: stackoverflow.com/q/26355847/1652710 Granted, this is in Obj-C, but hopefully you can translate it into a working solution for your issue in Swift. - Stakenborg

1 Answers

0
votes

You can't. The root view controller should be considered the visual representation of your application. To make this modal implies that your application itself is modal, which obviously isn't the case.

What you are really looking to be able to do is present the modal view controller as the first thing users see. This is not the same as being the root view controller.

Set the root view controller to a regular view controller (or navigation controller) and then push your modal view controller onto it. When the modal view controller is dismissed, your application may begin.