当应用程序终止时,使应用程序保存数据

时间:2017-10-23 16:45:08

标签: ios swift delegates

我已经知道如何保存,我可以在打开应用程序时自动加载它们。但我不知道如何在关闭时保存它们!我现在有一个按钮。我想保存一个值StreakNumber。但是,每当我尝试时,我都会失败。

在app delegate中我尝试说ViewController.AppSave(ViewController)。我收到一个错误:

  

源文件中的编辑器占位符   表达式解析为未使用的函数

如果我使用ViewController.AppSave() 我收到以下错误:

  

实例成员' SaveApp'不能用于类型' ViewController&#39 ;;你的意思是使用这种类型的值吗?

import UIKit
import UserNotifications

class ViewController: UIViewController {
    //TEST PLAY AREA//

    @IBAction func SaveButton(_ sender: Any) {
        SaveApp()
    }

    @IBAction func LoadButton(_ sender: Any) {
        LoadApp()
    }

    ///TEST PLAY AREA ENDS
    public var streakNumber = 0
    public var  streakNumberString = ""

    public var activated = false

    let defaults = UserDefaults.standard
    //Labels
    @IBOutlet var streakNumberLabel: UILabel!

    @IBOutlet var remainingTimeTextLabel: UILabel!

    @IBOutlet var remainingTimeNumberLabel: UILabel!
    //Buttons
    @IBAction func startButton(_ sender: Any) {
        Activate()

    }
    @IBAction func stopButton(_ sender: Any) {
        Deactivate()
        seconds = 59
        minutes = 59
        hours = 47

    }

    //Timer
    var timer = Timer()
    var seconds = 0
    var minutes = 0
    var hours = 0

    // Nitifications
    //END
    override func viewDidLoad() {
        super.viewDidLoad()

        LoadApp()

        // this enables notifications
        UNUserNotificationCenter.current().requestAuthorization(options:[.alert , .sound , .badge], completionHandler:{ didAllow, error in
        })
    }

    func Activate (){
        if activated == false {
            streakNumber += 1

        }
        activated = true
        streakNumberLabel.text = String(streakNumber)
    }

    func Deactivate (){
        if activated == true{
            activated = false
            ActivTimer()

            // Notification
            let content = UNMutableNotificationContent()
            content.title = " 10  secodns are up "
            content.subtitle = " yep time passed"
            content.body = " The 10 seconds are really up!!"
            content.badge = 1
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 600, repeats:false)
            let request = UNNotificationRequest(identifier: "timerDone", content: content , trigger: trigger)
            UNUserNotificationCenter.current().add(request,withCompletionHandler:nil)
        }
    }

    @objc func Clock(){
        seconds = seconds-1
        if seconds == -1{
            seconds = 59
            minutes = minutes-1
        }
        if minutes  == -1{
            minutes  = 59
            hours = hours-1
        }
        remainingTimeNumberLabel.text = (String(hours) + ":" + String(minutes) + ":" + String(seconds))
        if seconds == 0 && hours == 0 && minutes == 0 {

            timer.invalidate()

        }
    }

    func ActivTimer(){
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.Clock), userInfo: nil, repeats: true)
    }//SAVING STUFF DOESNT WORK
    //

    func SaveApp(){
        streakNumberString = String(streakNumber)
        defaults.set(streakNumberString, forKey: "streakNumbers")
        print("Saved" + String(defaults.integer(forKey: "streakNumbers")))

    }

    func LoadApp(){
        streakNumberString =  defaults.string(forKey: "streakNumbers")!
        print (defaults.integer(forKey: "streakNumbers") )
        streakNumber = Int(streakNumberString)!
        streakNumberLabel.text = String(streakNumber)
    }//
}

1 个答案:

答案 0 :(得分:0)

错误说明了一切。您无法在SaveApp()上调用ViewController。实际上,您需要ViewController的实例来自行调用SaveApp()

如果您希望ViewController实例将其数据保存在应用终止上,您可以使用AppDelegate的{​​{1}}方法发布通知,该方法会在应用正在进行时调用最小化和终止前:

applicationDidEnterBackground

然后您可以在func applicationDidEnterBackground(_ application: UIApplication) { NotificationCenter.default.post(Notification(name: Notification.Name("AppAboutToTerminateOrMinimize"))) } ViewController {订阅}

中订阅该通知
viewDidLoad()

或者正如评论中提到的rmaddy,您可以将以下代码放在NotificationCenter.default.addObserver(self, selector: #selector(AppSave), name: Notification.Name("AppAboutToTerminateOrMinimize"), object: nil) 的{​​{1}}中,并跳过ViewController部分:

viewDidLoad()

这样,当应用最小化/终止AppDelegate将发送通知时,NotificationCenter.default.addObserver(self, selector: #selector(AppSave), name: Notification(name: UIApplicationDidEnterBackgroundNotification, object: nil) 实例(如果有效)将收到该通知并将调用AppDelegate

您必须将ViewController方法注释为AppSave()

使用lowerCamelCase命名属性和方法/i AppSave()而不是@objc

是一个好习惯。