Disable the view controller when you click the Home button

I am building a fast application. I have several viewControllers. When someone is in a specific viewController and click the home button, I want to reject this ViewController. I do not want any actions in other view controllers. I found out when I press the home button after the Appdelagate function calls

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

Therefore, I managed to capture this action using Notifier on my view controller (viewDidLoad), as shown below.

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 View Control problem is not firing. I mean that no action will happen. How to handle this.

Note. . When I press the "home" button, the called function is myObserverMethod and is called ("I am also called").

?

1: , HomeVC ( ), .

2: , , .. . , HomeVC . , . :

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

. , .

+4
4

, , this , , -, UINavigationController, .

"" ". , , (navigationController!.pushViewController(controller, animated: false);, , , : popViewControllerAnimated(_:). VC, , (, , ).

:

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

. , , , , ..

0

viewController.dismissViewControllerAnimated(false, completion: nil)

if

if viewController.isKindOfClass(HomeVC.classForCoder())
0

Obj C..

vc

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


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

homebutton

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.resetAppToFirstController;
0

myObserverMethod:

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()
                }
            }
        }

, .,.

0

Source: https://habr.com/ru/post/1620485/


All Articles