gpt4 book ai didi

ios - 10 分钟后重置 Swift 应用程序

转载 作者:搜寻专家 更新时间:2023-10-31 19:38:27 29 4
gpt4 key购买 nike

我正在使用 Swift 制作小费计算器应用程序。我的目标是在 10 分钟后重置我的应用程序(换句话说,将我的所有标签设置为 $0.00 并将默认值设置回 NSUserDefaults)。

我将这 3 个函数放在我的 ViewController.swift 文件中:

func compareTimes(opens: NSDate?, closes: NSDate?) {
if(opens!.timeIntervalSinceReferenceDate-closes!.timeIntervalSinceReferenceDate>600) {
reset()
}
}
func openApp() {
openingTime = NSDate()
let defaults = NSUserDefaults.standardUserDefaults()
closingTime = defaults.objectForKey(closeKey) as! NSDate?
if (closingTime != nil) {
compareTimes(openingTime, closes: closingTime)
}
}

func closeApp() {
closingTime = NSDate()
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(closingTime, forKey: closeKey)
}

在我的 AppDelegate 中,我调用了其中两个方法:

func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
ViewController().closeApp()
}

func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
ViewController().openApp()
}

请注意,关闭应用程序时会记录时间,打开应用程序时也会记录时间。比较这些时间,如果 10 分钟过去了,则调用 reset()

我的问题是当 reset() 被调用时,我所有代表 UILabels 和 UITextFields 的变量都变成 nil,我得到一个错误

fatal error: unexpectedly found nil while unwrapping an Optional value.

我的reset()方法供引用:

func reset() {
billField.text=""
tipLabel.text = "+ "+formatter.stringFromNumber(0)!
totalLabel.text = formatter.stringFromNumber(0)
total2.text = formatter.stringFromNumber(0)
total3.text = formatter.stringFromNumber(0)
total4.text = formatter.stringFromNumber(0)

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("15", forKey: key1)
defaults.setObject("18", forKey: key2)
defaults.setObject("20", forKey: key3)
defaults.synchronize()
percentages.append(0.1)
percentages.append(0.1)
percentages.append(0.1)
percentButton.setTitle("15%", forSegmentAtIndex: 0)
percentButton.setTitle("18%", forSegmentAtIndex: 1)
percentButton.setTitle("20%", forSegmentAtIndex: 2)
}

最佳答案

你的问题是,当你说 ViewController().closeApp() 时,你正在分配一个新的 ViewController 实例并调用 closeApp在该实例上运行。由于您没有从 Storyboard 中实例化该实例,因此没有任何导出被附加并且您得到一个 nil 引用异常。

您需要调用现有 ViewController 实例上的方法。你可以使用这样的东西:

func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
if let viewController = application.keyWindow?.rootViewController as? ViewController {
viewController.closeApp()
}
}


func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
if let viewController = application.keyWindow?.rootViewController as? ViewController {
viewController.closeApp()
}
}

关于ios - 10 分钟后重置 Swift 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34553287/

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