ios - 让应用程序在终止时保存数据

标签 ios swift delegates

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

当在应用程序委托(delegate)中时,我尝试说ViewController.AppSave(ViewController)。我收到错误:

Editor placeholder in source file Expression resolves to an unused function

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

Instance member 'SaveApp' cannot be used on type 'ViewController'; did you mean to use a value of this type instead?

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)
    }//
}

最佳答案

错误说明了一切。您无法在 ViewController 上调用 SaveApp()。您实际上需要一个 ViewController 实例来调用 SaveApp() 本身。

如果您希望 ViewController 实例在应用程序终止时保存其数据,您可以从 AppDelegateapplicationDidEnterBackground 方法发布通知,每当应用程序最小化时以及终止之前都会调用它:

func applicationDidEnterBackground(_ application: UIApplication) {

    NotificationCenter.default.post(Notification(name: Notification.Name("AppAboutToTerminateOrMinimize")))
}

然后您可以在 ViewControllerviewDidLoad() 中订阅该通知:

NotificationCenter.default.addObserver(self,
                                       selector: #selector(AppSave),
                                       name: Notification.Name("AppAboutToTerminateOrMinimize"),
                                       object: nil)

或者正如rmaddy在评论中提到的,您可以将以下代码放入ViewControllerviewDidLoad()中并跳过AppDelegate部分:

NotificationCenter.default.addObserver(self,
                                       selector: #selector(AppSave),
                                       name: Notification(name: UIApplicationDidEnterBackgroundNotification,
                                       object: nil)

这样,当应用程序最小化/终止时,AppDelegate 将发送通知,ViewController 实例(如果处于事件状态)将接收它并调用 AppSave()

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

最好使用小驼峰命名法来命名属性和方法/即 appSave() 而不是 AppSave()

关于ios - 让应用程序在终止时保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46894471/

相关文章:

ios - Objective C MFMailComposeViewController 取消 -> 取消按钮不起作用

ios - 异步函数返回正常,但数组变为空

ios - fatal error : Index out of range in swift while taking options from uipicker

swift - 在静态 UITableViewController 之上的 UIView

swift - 如何使滑动手势识别器同时检测左右方向?

javascript - 在鼠标悬停时显示/隐藏 div 不起作用

ios - 受密码保护的 iOS 自定义设置包

ios - 使用 PayPal SDK 在结账时得到这个 "delegate does not respond to selector adjust amounts"

c++ - 代码完成 2ed,组合和委托(delegate)

objective-c - objective-c 中的多个委托(delegate)