gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:03:48 25 4
gpt4 key购买 nike

我已经知道如何保存内容,并且可以在打开应用程序时自动加载它们。但我不知道如何在关闭时拯救它们!我现在有一个按钮。我试图仅保存一个值,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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com