title: How to load initial view controller in Swift?
body: <p>I am developing an application for a <strong>username-password</strong> based system. My initial view controller is named <code>LoginPageViewController</code>. If the user logs in successfully, a navigation controller named <code>MyNavigationController</code> is loaded. All view controllers except <code>LoginPageViewController</code> are accessed via a side-menu I found here: <a href="https://github.com/evnaz/ENSwiftSideMenu" rel="nofollow">Side Menu Library</a> by the help of <code>MyNavigationController</code>.</p>
  
  <p>View settings of my side menu are described in <code>MyMenuTableViewController</code> and if any of table items is selected, relevant view controller is loaded from <code>MyMenuTableViewController</code>. The problem is, I have also added "Logout" item to my side menu (or menu table, whatever you call) and if this item is selected, it makes an <strong>HTTP POST</strong> request to server to log out and should load the <code>LoginPageViewController</code> which is my initial view controller (which is not embedded in <code>MyNavigationControlelr</code>).</p>
  
  <p>The problem is, although the logout request is handled successfully, I have been encountering problems while loading my initial view controller (<code>LoginPageViewController</code>). </p>
  
  <p>When I try this when Logout is selected:</p>
  
  <pre><code>self.presentViewController(destViewController, animated: true, completion: nil)
  </code></pre>
  
  <p>LoginPageViewController is being loaded but I get an message like:<code>2014-10-30 16:59:26.038 iOpsGenie[2973:66509] Presenting view controllers on detached view controllers is discouraged &lt;iOpsGenie.MyMenuTableViewController: 0x7f85e8fc3f10&gt;.
  </code>
  and if I click any where on Login Page, my view disappears and only a white screen is left.</p>
  
  <p>Then I tried implementing a method</p>
  
  <pre><code> func resetAppToFirstController() {
       self.window?.rootViewController = LoginPageViewController(nibName: nil, bundle: nil)
       }
  </code></pre>
  
  <p>in <strong>AppDelegate.swift</strong> and call it when Logout item is being selected:</p>
  
  <pre><code>let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
  appDelegate.resetAppToFirstController()
  </code></pre>
  
  <p>Then I got an error in second line of viewDidLoad function of LoginPageViewController like:</p>
  
  <pre><code>fatal error: unexpectedly found nil while unwrapping an Optional value
  </code></pre>
  
  <p>I know it became a very long question. I may be counted as a newbie on Swift and iOS programming and I tried to state the problem to prevent irrelevant responses. I really wonder what I am missing. </p>
  
  <p>Best regards</p>