2015-09-04 24 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。但是,您的自定義對象必須是可歸檔的。

相關問題