0

I am trying to call my menu view inside my view controller when the home button is pressed, or for that matter when the user gets a phone call, etc...

My goal is to call the function: toggleMenu() that is inside the QuestionViewController. Here's the code:

class QuestionViewController: UIViewController, MFMailComposeViewControllerDelegate {

///////////

    func toggleMenu() {

    // Show or hide menu

    if menuStackView.isHidden == true {

        // Show the menu
        print("Showing Menu")
        // Update labels
        questionNumberMenuLabel.text = questionNumberLabel.text
        endTimer()
        menuStackView.isHidden = false
        animateInMenu()


    } else {

        // Hide the menu
        animateOutMenu()

    }
}

I do believe I should utilize the following method found in the AppDelegate.swift file:

    func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

If I'm on the right track I need help calling the toggleMenu() function of the QuestionViewController class in this method. How do I do that?

Thanks so much!

Jared L.
  • 207
  • 2
  • 13

1 Answers1

1

Use the NotificationCenter and listen for UIApplicationWillResignActiveNotification. The system broadcasts that notification as well as calling your app delegate's applicationWillResignActive method (assuming you have one.)

Listen for notifications (using the method addObserver(forName:object:queue:using:) in your viewDidLoad. If you don't need to support iOS versions < 9.0, you don't need to worry about calling removeObserver - the system takes care of it for you.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks! I'm new to Xcode & Swift, so some of this is still over my head. I tried adding this line of code: override func viewDidLoad() { super.viewDidLoad() // Listen to notifications addObserver(observer: NSObject, forKeyPath: String, options: NSKeyValueObservingOptions, context: UnsafeMutableRawPointer but this isn't the same as your observer function. Also I don't know how to setup an app delegate for the applicationWillResignActive method that you mentioned earlier. I've used prepareforsegue to send data between VCs – Jared L. Mar 30 '18 at 12:54
  • Don't more than a code fragment in comments. The lack of formatting makes it unreadable. Go back and edit your question, add and `##EDIT:` tag at the bottom, and show your new code there – Duncan C Mar 30 '18 at 14:01
  • Thanks! I'm new to Xcode & Swift, so some of this is still over my head. I tried adding this line of code. but this isn't the same as your observer function. Also I don't know how to setup an app delegate for the applicationWillResignActive method that you mentioned earlier. I've used prepareforsegue to send data between VCs ##EDIT: override func viewDidLoad() { super.viewDidLoad() // Listen to notifications addObserver(observer: NSObject, forKeyPath: String, options: NSKeyValueObservingOptions, context: UnsafeMutableRawPointer – Jared L. Mar 30 '18 at 14:12
  • Shoot, I can’t figure this out. Let me try this instead... (see next comment) – Jared L. Mar 30 '18 at 14:13
  • override func viewDidLoad() { super.viewDidLoad() // Listen to notifications addObserver(observer: NSObject, forKeyPath: String, options: NSKeyValueObservingOptions, context: UnsafeMutableRawPointer – Jared L. Mar 30 '18 at 14:13
  • More importantly, I think I found a solution, what are your thoughts on this?: https://stackoverflow.com/questions/9011868/whats-the-best-way-to-detect-when-the-app-is-entering-the-background-for-my-vie?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Jared L. Mar 30 '18 at 14:15