如何使用applicationWillResignActive来调用视图控制器的功能

时间:2018-03-29 23:13:06

标签: swift xcode delegates appdelegate

我在按下主页按钮时尝试在我的视图控制器中调用我的菜单视图,或者当用户接到电话时等等...

我的目标是调用QuestionViewController中的函数:toggleMenu()。这是代码:

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

    }
}

我相信我应该使用AppDelegate.swift文件中的以下方法:

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

如果我在正确的轨道上,我需要帮助调用此方法中QuestionViewController类的toggleMenu()函数。我该怎么做?

非常感谢!

1 个答案:

答案 0 :(得分:1)

使用CREATE TABLE temp as Select top 2 id, Gender, max(capacity) as maxcap from table where Gender ='F' group by id, Gender CREATE TABLE temp2 as Select top 2 id, Gender, max(capacity) as maxcap from table where Gender ='M' group by id, Gender select top 3 id, gender, maxcap from ( Select id, Gender, maxcap from temp union Select Gender, maxcap from temp2 ) t order by t.maxcap desc 并收听NotificationCenter。系统广播该通知以及调用您的app delegate的applicationWillResignActive方法(假设您有一个。)

收听通知(使用viewDidLoad中的方法UIApplicationWillResignActiveNotification。如果您不需要支持iOS版本< 9.0,则无需担心调用removeObserver - 系统为你照顾它。