大佬教程收集整理的这篇文章主要介绍了ios – 10分钟后重置Swift应用程序,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我将这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) }
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并且出现错误
func reset() { billField.text="" tipLabel.text = "+ "+formatter.StringFromnumber(0)! @R_902_10586@lLabel.text = formatter.StringFromnumber(0) @R_902_10586@l2.text = formatter.StringFromnumber(0) @R_902_10586@l3.text = formatter.StringFromnumber(0) @R_902_10586@l4.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实例上调用方法.你可以使用这样的东西:
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应用程序所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。