我将这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函数.由于您没有从故事板中实例化该实例,因此没有附加任何插件,您将获得零引用异常.
您需要在现有VIEwController实例上调用方法.你可以使用这样的东西:
func applicationDIDEnterBackground(application: UIApplication) { // Use this method to release shared resources,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应用程序全部内容,希望文章能够帮你解决ios – 10分钟后重置Swift应用程序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)