如何在swift 4中的app delegate中访问视图控制器功能

时间:2018-03-24 02:45:53

标签: ios swift function uiviewcontroller appdelegate

  

Xcode给了我错误"实例成员' pauseGame322'不能用于类型' ViewController&#39 ;;你的意思是使用这种类型的值吗?"

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if  appropriate. See also applicationDidEnterBackground:.
    ViewController.pauseGame322(ViewController)
}

1 个答案:

答案 0 :(得分:1)

尝试在视图控制器中添加观察者

NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillTerminate),
name: .UIApplicationWillTerminate,
object: nil)

回调

@objc func applicationWillTerminate() {

     self.pauseGame322()

}