title: Dismiss view controller when the home button pressed
body: <p>I am creating an application with swift. I have multiple viewControllers. When Someone in a particular viewController and press the home button, then I want to dismiss that ViewController. I don't want any action in other view controllers. I found out when I press the home button following Appdelagate func will call</p>
  
  <pre><code> func applicationDidEnterBackground(application: UIApplication) {
          print("Enter my Guess")
          // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
          // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
      }
  </code></pre>
  
  <p>So I manage to capture this action using Notifier on my view controller (viewDidLoad) like the following</p>
  
  <pre><code>NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("myObserverMethod:"), name:UIApplicationDidEnterBackgroundNotification, object: nil)
  </code></pre>
  
  <p>And </p>
  
  <pre><code>func myObserverMethod(notification : NSNotification) {
  
          if let viewControllers = navigationController?.viewControllers {
              for viewController in viewControllers {
                  // some process
                  if viewController.isKindOfClass(HomeVC) {
                      print("I am calledr")
  //                    dismissViewControllerAnimated(false, completion: nil)
  
                  }
              } 
          }
      }
  </code></pre>
  
  <p>Now my problem View controller not dismissing. I mean no action happen. How to handle this.</p>
  
  <p><strong>Note :</strong> Whenever I press the home button myObserverMethod func called and  print("I am calledr") also called. </p>
  
  <p>What I am doing wrong?</p>
  
  <p><strong>Edit 1:</strong>
  I want to dismiss if I am in HomeVC(View controller name) otherwise no need to dismiss the view controller.</p>
  
  <p><strong>Edit 2:</strong>
  I found out why it always enters to that if.. Condition. The reason is always it keeps HomeVC in the memory. So always enters into that if. So I add another one condition inside of that like the following.</p>
  
  <pre><code> if (viewController.isViewLoaded() &amp;&amp; viewController.view.window != nil){
  
                          print("I am calledr")
                          viewController.dismissViewControllerAnimated(false, completion: nil)
  //                          self.presentingViewController!.dismissViewControllerAnimated(false, completion: nil)
                         // viewController.dismissViewControllerAnimated(false, completion: nil)
                      }
  </code></pre>
  
  <p>No no action happened. I mean the view controller not dismissed.</p>