Amsheer
Amsheer

Reputation: 7131

Dismiss view controller when the home button pressed

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.

Upvotes: 4

Views: 2853

Answers (4)

NerdyTherapist
NerdyTherapist

Reputation: 530

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.

Upvotes: 0

iHardikTrivedi
iHardikTrivedi

Reputation: 559

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

Upvotes: 0

Moin Shirazi
Moin Shirazi

Reputation: 4425

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;

Upvotes: 0

Mihir Mehta
Mihir Mehta

Reputation: 13833

This line should work

viewController.dismissViewControllerAnimated(false, completion: nil)

you can change your if condition to

if viewController.isKindOfClass(HomeVC.classForCoder())

Upvotes: 0

Related Questions