Xcode9学习笔记72 - 退出系统前的事件处理

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //获得一个应用实例,应用实例的核心作用是提供程序运行期间的控制和协作,每个程序必须有且仅有一个应用实例
        let app = UIApplication.shared
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.doSomething(_:)), name: .UIApplicationWillResignActive, object: app)
    }
    
    @objc func doSomething(_ sender:AnyObject) {//响应退出事件,退出前保存用户数据
        print("Saving data before exit.")
    }

  

posted on 2017-11-24 07:39  业余极客  阅读(244)  评论(0编辑  收藏  举报

导航