Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 退出应用程序时,在UserDefault中存储标签值(日期)_Swift_Uilabel_Nsuserdefaults_Appdelegate - Fatal编程技术网

Swift 退出应用程序时,在UserDefault中存储标签值(日期)

Swift 退出应用程序时,在UserDefault中存储标签值(日期),swift,uilabel,nsuserdefaults,appdelegate,Swift,Uilabel,Nsuserdefaults,Appdelegate,我的目标是存储用户退出应用程序时在timeLabel上显示的日期。当应用程序再次运行时,标签的显示时间应与用户退出应用程序的时间相同 我知道我必须以某种方式将时间标签保存到用户第一次离开应用程序时的默认值。我也不知道我是否需要对AppDelegate做些什么 import UIKit class ViewController: UIViewController { var currentDateTime = Date() var timeLabel = UILabel() let defaul

我的目标是存储用户退出应用程序时在
timeLabel
上显示的日期。当应用程序再次运行时,标签的显示时间应与用户退出应用程序的时间相同

我知道我必须以某种方式将
时间标签
保存到用户第一次离开应用程序时的默认值。我也不知道我是否需要对AppDelegate做些什么

import UIKit

class ViewController: UIViewController {
var currentDateTime = Date()
var timeLabel = UILabel()
let defaults = UserDefaults.standard


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

     let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .medium

    view.addSubview(timeLabel)
    timeLabel.translatesAutoresizingMaskIntoConstraints = false
    timeLabel.backgroundColor = .systemRed
     timeLabel.text = "\(dateFormatter.string(from: currentDateTime))"

    NSLayoutConstraint.activate([

        timeLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
        timeLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
        timeLabel.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4, constant: 49),
        timeLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.4, constant: 49),



    ])

    defaults.set(timeLabel.text, forKey: "label")
}


}

在后台回调中添加一个监听器,你可以在vc中这样做

NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: .main) { [weak self] notification in 
   defaults.set(Date(), forKey: "lastDate")
}

或者在
AppDelegate
method

中,问题在于您没有从UserDefaults加载字符串。因此,在视图控制器中创建一个方法,在应用程序进入前台时执行,尝试读取字符串并用它更新标签

@objc func willEnterForeground(_ notification: Notification) {
    if let string = defaults.string(forKey: "label") {
        timeLabel.text = string
    }
}
要保存用户离开应用程序或进入后台的日期,您可以为iOS12或更早版本添加观察者,请使用
UIApplication.willResignActiveNotification
,对于iOS13或更高版本,请使用
UIScene.willDeactivateNotification
。然后添加一个选择器来处理停用

UIScene.willEnterForegroundNotification
UIApplication.willEnterForegroundNotification

将这些观察者添加到视图控制器内部
viewDidLoad
方法:

if #available(iOS 13.0, *) {
    NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIScene.willDeactivateNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIScene.willEnterForegroundNotification, object: nil)
} else {
    NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIApplication.willResignActiveNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
}
创建将字符串保存到
UserDefaults
的方法。不清楚您的目的是在用户每次离开应用程序时保存它,还是只想第一次保存它。要在每次应用程序进入后台时保存它,请执行以下操作:

@objc func willResignActive(_ notification: Notification) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .medium
    dateFormatter.timeStyle = .medium
    let string = dateFormatter.string(from: Date())
    defaults.set(string, forKey: "label")
}

此代码仅在第一次保存它。我想在用户每次离开应用程序时保存它。很抱歉,我没有说得很清楚。这是我在调试部分得到的。@MitchMustain它正在保存它,以确保从后台返回时可能不会更新它。您可以为willEnterForegroundNotification添加观察者,并在出现时更新标签