0

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.

Adam Carter
  • 4,741
  • 5
  • 42
  • 103
  • I've provided an answer to a similar question in another question: http://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 Apr 15 '15 at 16:42

1 Answers1

0

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.

Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • So how and where do I manage the log in view controller? For example, if the VC is modally presented by the notification I should dismiss it modally, and if it’s presented as a navigation controller I want to push on the tab bar controller – Adam Carter Feb 10 '15 at 16:48
  • Should I use a protocol in log in view controller and manage the dismissal in the presenting view controller through a delegate? – Adam Carter Feb 10 '15 at 16:49
  • I'm unclear what you're asking. When your application starts, push the modal view controller onto your root view controller. When it dismisses, handle the result and decide where to go. – Ian MacDonald Feb 10 '15 at 16:49
  • My root view controller is a tab bar controller, and presenting the view controller on top of it throws the error `2015-02-10 17:02:23.338 MyApp[242:13441] Warning: Attempt to present on whose view is not in the window hierarchy!` – Adam Carter Feb 10 '15 at 17:03
  • You haven't assigned your `MainTabBarController` as the root view controller and/or you have not yet pushed it. You cannot push a modal view controller onto a view controller that has not yet been pushed to the screen. – Ian MacDonald Feb 10 '15 at 17:05