2015-09-04 60 views
0

我正在做一个减肥应用程序,可以节省你每天的体重。我需要知道如何保存当前日期的重量,然后使文本字段在第二天为空。我使用NSUserDefault保存了文本字段。我试图做其他的健身应用程序有那里的用户界面,你可以去看看前几天,看看保存的数据。有人能指引我走向正确的方向吗?谢谢!我将如何保存特定日期的文本字段?

override func didMoveToView(view: SKView) { 

    let stringKey = NSUserDefaults.standardUserDefaults() 
    textFieldWeight.text = stringKey.stringForKey("saveWeight") 

    addTextFieldForWeight() 


    } 

    func addTextFieldForWeight() { 

    textFieldWeight.borderStyle = UITextBorderStyle.Line 
    textFieldWeight.textColor = UIColor.whiteColor() 
    textFieldWeight.autocorrectionType = UITextAutocorrectionType.Yes 
    textFieldWeight.keyboardType = UIKeyboardType.NumberPad 
    textFieldWeight.delegate = self 
    textFieldWeight.backgroundColor = UIColor.clearColor() 
    textFieldWeight.returnKeyType = UIReturnKeyType.Done 
    textFieldWeight.contentVerticalAlignment = UIControlContentVerticalAlignment.Center 
    self.view?.addSubview(textFieldWeight) 
    } 



    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 





     if node.name == "next" { 

     let nextDay = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitDay,value: 1,toDate: 
      NSDate(),options: nil) 
     let formatter = NSDateFormatter() 
     formatter.dateStyle = NSDateFormatterStyle.FullStyle 



     let dateString = formatter.stringFromDate(nextDay!) 
     dateLabel.text = String(dateString) 

    } 







    if node.name == "before" { 

     let earlyDate = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitDay,value: -1,toDate: 
      NSDate(),options: NSCalendarOptions.WrapComponents) 
     let formatter = NSDateFormatter() 
     formatter.dateStyle = NSDateFormatterStyle.FullStyle 



     let dateString = formatter.stringFromDate(earlyDate!) 
     dateLabel.text = String(dateString) 

     } 
      } 
+1

你需要保存每天病史?我的意思是每一天的体重。 –

+0

是的,我可能会改变它的卡路里,而不是体重,但我想保存每一天。 – coding22

+1

对于必须使用[源码](http://www.appcoda.com/sqlite-database-ios-app-tutorial/)或[核心数据](https://developer.apple.com/library/ios /documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html) –

回答

1

您可能想要查看核心数据结构,或者为您可以存档的数据对象创建一个类。在这样一个小盒子里解释很多,但在Apple的帮助文档中查找核心数据和创建和提取档案。

如果你想有一个简单的解决方案,尽量只使用一个NSMutableArray,其中阵列中的每个元素可以是包含重量一个NSDate以及NSNumber的对象的自定义对象。然后,您可以将此数组保存到NSUserDefaults。但是,您的自定义对象必须是可归档的。

相关问题