ViewController 更改后计数器重置为零
Counter reset to zero after change of ViewController
我已经在我的应用程序中实现了一个计数器,并且可以正常工作。然后,当我更改 ViewController
(当我 运行 我的应用程序时),我的计数器自动重置为 0。我希望我的计数器在我使用该应用程序时继续。
感谢您的帮助:)
import UIKit
class ViewController: UIViewController
{
/// Label
private var customLabel : UILabel?
/// MAximum Count to which label will be Updated
private var maxCount : Int?
/// Count which is currently displayed in Label
private var currentCount : Int?
/// Timer To animate label text
private var updateTimer : Timer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
customLabel = UILabel()
customLabel?.textColor = .white
customLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 25)
customLabel?.textAlignment = .center
/// Add label to View
addConstraints()
/// Start Timer
DispatchQueue.main.async {
self.maxCount = 600
self.currentCount = 1
self.updateTimer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(ViewController.updateLabel), userInfo: nil, repeats: true)
}
}
@objc func updateLabel() {
self.customLabel?.text = String(currentCount!)
currentCount! += 1
if currentCount! > maxCount! {
/// Release All Values
self.updateTimer?.invalidate()
self.updateTimer = nil
self.maxCount = nil
self.currentCount = nil
}
}
func addConstraints(){
/// Add Required Constraints
self.view.addSubview(customLabel!)
customLabel?.translatesAutoresizingMaskIntoConstraints = false
customLabel?.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 195).isActive = true
customLabel?.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50).isActive = true
customLabel?.heightAnchor.constraint(equalToConstant: 50).isActive = true
customLabel?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 310).isActive = true
}
}
实际发生的事情是,每当你回到你的 ViewController 你正在创建一个新的计数器你需要做的是在 viewDidLoad() 之外声明你的计数器,但要确保你的 ViewController是您的应用程序的根 ViewController,它永远不会被取消初始化,否则您的应用程序将崩溃。
我的建议是在你的根控制器中实现计时器,它不会死,直到你的应用程序像你的导航 Controller/Tab 控制器一样被终止,并在 'ViewController' 出现时不断更新标签。
这就是它在 Controller 死机时的工作方式。
你的计数器实际上是你的视图控制器的一部分,它存在于内存中,当你重新启动你的应用程序时(我假设来自 Xcode)。它正在从设备内存中删除您的应用程序,这就是您丢失计时器计数的原因。
您能做的最好的事情就是将计时器计数保存到用户默认值(将其保存到磁盘),这意味着当您重新打开您的应用程序时,您可以从用户默认值中获取它并继续更改当前计数
声明这两个变量并让:
var defaults = UserDefaults.standard
let countKey = "CountKey"
每当您更新计数器时添加这行代码:
defaults.set(currentCount, forKey: countKey)
要取回您的值,请使用:
defaults.integer(forKey: countKey)
我已经在我的应用程序中实现了一个计数器,并且可以正常工作。然后,当我更改 ViewController
(当我 运行 我的应用程序时),我的计数器自动重置为 0。我希望我的计数器在我使用该应用程序时继续。
感谢您的帮助:)
import UIKit
class ViewController: UIViewController
{
/// Label
private var customLabel : UILabel?
/// MAximum Count to which label will be Updated
private var maxCount : Int?
/// Count which is currently displayed in Label
private var currentCount : Int?
/// Timer To animate label text
private var updateTimer : Timer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
customLabel = UILabel()
customLabel?.textColor = .white
customLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 25)
customLabel?.textAlignment = .center
/// Add label to View
addConstraints()
/// Start Timer
DispatchQueue.main.async {
self.maxCount = 600
self.currentCount = 1
self.updateTimer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(ViewController.updateLabel), userInfo: nil, repeats: true)
}
}
@objc func updateLabel() {
self.customLabel?.text = String(currentCount!)
currentCount! += 1
if currentCount! > maxCount! {
/// Release All Values
self.updateTimer?.invalidate()
self.updateTimer = nil
self.maxCount = nil
self.currentCount = nil
}
}
func addConstraints(){
/// Add Required Constraints
self.view.addSubview(customLabel!)
customLabel?.translatesAutoresizingMaskIntoConstraints = false
customLabel?.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 195).isActive = true
customLabel?.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50).isActive = true
customLabel?.heightAnchor.constraint(equalToConstant: 50).isActive = true
customLabel?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 310).isActive = true
}
}
实际发生的事情是,每当你回到你的 ViewController 你正在创建一个新的计数器你需要做的是在 viewDidLoad() 之外声明你的计数器,但要确保你的 ViewController是您的应用程序的根 ViewController,它永远不会被取消初始化,否则您的应用程序将崩溃。
我的建议是在你的根控制器中实现计时器,它不会死,直到你的应用程序像你的导航 Controller/Tab 控制器一样被终止,并在 'ViewController' 出现时不断更新标签。
这就是它在 Controller 死机时的工作方式。
你的计数器实际上是你的视图控制器的一部分,它存在于内存中,当你重新启动你的应用程序时(我假设来自 Xcode)。它正在从设备内存中删除您的应用程序,这就是您丢失计时器计数的原因。
您能做的最好的事情就是将计时器计数保存到用户默认值(将其保存到磁盘),这意味着当您重新打开您的应用程序时,您可以从用户默认值中获取它并继续更改当前计数
声明这两个变量并让:
var defaults = UserDefaults.standard
let countKey = "CountKey"
每当您更新计数器时添加这行代码:
defaults.set(currentCount, forKey: countKey)
要取回您的值,请使用:
defaults.integer(forKey: countKey)