4

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

 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.
    }

So I manage to capture this action using Notifier on my view controller (viewDidLoad) like the following

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("myObserverMethod:"), name:UIApplicationDidEnterBackgroundNotification, object: nil)

And

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)

                }
            } 
        }
    }

Now my problem View controller not dismissing. I mean no action happen. How to handle this.

Note : Whenever I press the home button myObserverMethod func called and print("I am calledr") also called.

What I am doing wrong?

Edit 1: I want to dismiss if I am in HomeVC(View controller name) otherwise no need to dismiss the view controller.

Edit 2: 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.

 if (viewController.isViewLoaded() && viewController.view.window != nil){

                        print("I am calledr")
                        viewController.dismissViewControllerAnimated(false, completion: nil)
//                          self.presentingViewController!.dismissViewControllerAnimated(false, completion: nil)
                       // viewController.dismissViewControllerAnimated(false, completion: nil)
                    }

No no action happened. I mean the view controller not dismissed.

Amsheer
  • 7,046
  • 8
  • 47
  • 81

4 Answers4

0

This line should work

viewController.dismissViewControllerAnimated(false, completion: nil)

you can change your if condition to

if viewController.isKindOfClass(HomeVC.classForCoder())
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • I added this code. It dismiss whatever view controller is visible. Only HomVC is not dismissing. Could you please check my code my for loop and if condition. What I am doing wrong? – Amsheer Dec 17 '15 at 06:36
  • i have edited the answer , see if this works change your if condition – Mihir Mehta Dec 17 '15 at 06:39
  • I tried no change in output still all the view controller dismissing HomeVC not dismissing. – Amsheer Dec 17 '15 at 06:43
  • are you using viewcontroller.dismiss or direct dismiss .... ??? from your code it looks like you are using dismissViewControllerAnimated not viewController. dismissViewControllerAnimated – Mihir Mehta Dec 17 '15 at 06:45
  • Before I read your answer I just use dismissviewcontroller. After Your answer I am using viewcontroller.dismissViewcontroller. My question why it is entering to that condition, because I added HomeVC.classForCoder() – Amsheer Dec 17 '15 at 06:47
  • Could you please check my Edit 2. – Amsheer Dec 21 '15 at 05:50
0

For Obj C .. you can convert for swift

In app delegate create a method to allocate memory to vc again

- (void)resetAppToFirstController
{
    UIViewController *obj = [[MainMenuViewController alloc] init];


    //Create Navigation Controller to Hold View Controller
    UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:obj];
   [self.window setRootViewController:navigationController];
}

and on my homebutton button action

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.resetAppToFirstController;
Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
0

Just simple use below code in myObserverMethod method:

if let viewControllers = navigationController?.viewControllers {
            for var i = 0; i < viewControllers.count; i++ {
                // some process
                if ((self.navigationController?.viewControllers[i].isKindOfClass(HomeVC)) != nil) {
                    print("I am calledr")
                    self.navigationController?.viewControllers[i].removeFromParentViewController()
                }
            }
        }

I hope this works . . .

iHardikTrivedi
  • 559
  • 3
  • 16
0

Updated

Instead of using a modal view controller, which is what I assumed initially, where this would apply, you are apparently using a UINavigationController, which is documented here.

Items on the navigation controller's so-called stack are being "pushed" and popped". So, just as you push the controller like you do in your comment (navigationController!.pushViewController(controller, animated: false);, you have to pop it in order to get rid of it, like so: popViewControllerAnimated(_:). This pops the topmost VC, but doesn't work when your controller already is the root (and thus, only) one.

Your code should look something like this:

if viewController.isKindOfClass(HomeVC) {
                    print("I am calledr")
                    navigationController.popViewControllerAnimated(false)
            }

Note: I don't know exactly what your code and setup looks like, so you might have to try around a bit for it to work, depending on whether it's the root view, etc.

NerdyTherapist
  • 520
  • 5
  • 15
  • If I add this and press home button app crashed. – Amsheer Dec 18 '15 at 12:34
  • How are you presenting that view controller, modally? Or is it inside a Navigation Controller? (I think you would need either of these to get the functionality you want) – NerdyTherapist Dec 18 '15 at 12:42
  • Use this codes let storyboard = UIStoryboard(name: "Main", bundle: nil) let controller = storyboard.instantiateViewControllerWithIdentifier("HomeVC"); navigationController!.pushViewController(controller, animated: false); – Amsheer Dec 18 '15 at 12:44
  • Could you please check my Edit 2. – Amsheer Dec 21 '15 at 05:50