NSUserDefault问题

时间:2016-09-13 17:14:06

标签: swift nsuserdefaults

我正在创建一个潜水日志应用。当有人选择新潜水时,他们会收到两个选项的警报:“空白模板”或“将上次潜水用作模板”。 问题在于后期,最后一次潜水作为模板。

每次潜水我都会将数据记录到NSUserDefaults。因此,当有人想要最后一次潜水作为模板时,我只需调用每个默认值并填充表格视图。

但是,我在第一个字段,即diveSiteTextfield.text上收到一个nil错误,这让我很困惑:

以下是将数据保存到NSUserDefaults

的位置

如您所见,我添加了一个打印命令来测试潜水点字段是否为零,但它显示的是正确的数据。 diveSiteTextfield.text不是零。

import UIKit
import Parse


var diveUUID = NSUUID().UUIDString

class DiveDetails1VC: UIViewController, UITextFieldDelegate, UITableViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource
{
    let userDefault = NSUserDefaults.standardUserDefaults()

    @IBOutlet weak var ratingControl: DiveSiteRating!
    @IBOutlet weak var diveSiteTextfield: UITextField!
    @IBOutlet weak var diveDurationTextfield: UITextField!

...

func toDive2NewParse () {

    let diveObject = PFObject(className: "divelog")

        diveObject["diveNumber"]        = diveNumberInt
        diveObject["diveSite"]          = diveSiteTextfield.text
        diveObject["diveDuration"]      = diveDurationTextfield.text
        diveObject["maxDepth"]          = diveMaxDepthTextfield.text
        diveObject["diveDate"]          = diveDateTextfield.text
        diveObject["diveTime"]          = diveTimeTextfield.text
        diveObject["bodyOfWater"]       = diveBodyOfWaterTextfield.text
        diveObject["diveBuddy"]         = diveBuddyTextfield.text
        diveObject["diveCity"]          = diveCityTextfield.text
        diveObject["maxDepthSymbol"]    = diveMaxDepthSymbolTextField.text
        diveObject["diveCountry"]       = diveCountryTextField.text
        diveObject["uuid"] = "\(diveUUID)"

        //        diveObject["diveRating"]          = ratingControl


            diveObject["entryFrom"] = "Divelog"

            if PFUser.currentUser()!.username == nil {
                diveObject["username"] = "Not Signed in"
            } else {
            diveObject["username"] = PFUser.currentUser()!.username
            }
            diveObject.saveEventually()

        userDefault.setObject(diveNumberInt, forKey: "diveNumber")
        userDefault.setObject(diveSiteTextfield.text, forKey: "diveSite")
        userDefault.setObject(diveDurationTextfield.text!, forKey: "diveDuration")
        userDefault.setObject(diveMaxDepthTextfield.text, forKey: "diveDepth")
        userDefault.setObject(diveMaxDepthSymbolTextField.text, forKey: "symbol_maxDepth")
        userDefault.setObject(diveDateTextfield.text, forKey: "entryDate")
        userDefault.setObject(diveTimeTextfield.text, forKey: "entryTime")
        userDefault.setObject(diveCityTextfield.text, forKey: "diveCity")
        userDefault.setObject(diveCountryTextField.text, forKey: "diveCountry")
        userDefault.setObject(diveBodyOfWaterTextfield.text, forKey: "bodyOfWater")
        userDefault.setObject(diveBuddyTextfield.text, forKey: "diveBuddy")
        userDefault.setObject(diveUUID, forKey: "uuid")
        userDefault.synchronize()

    print("DiveSite is ...\(userDefault.objectForKey("diveSite"))")
}

以下是检索“最后一次潜水”的UIAlertAction:

@IBAction func addNewDive (sender: AnyObject) {

    let alertController = UIAlertController(title: "New Dive", message: "Choose a template", preferredStyle:UIAlertControllerStyle.ActionSheet)
    let buttonOne = UIAlertAction(title: "Blank template", style: .Default, handler: { (action) -> Void in


        self.performSegueWithIdentifier("segueNewDive1", sender: self)

        print("Button One Pressed")
    })
    let buttonTwo = UIAlertAction(title: "Use last dive as template", style: .Default, handler: { (action) -> Void in

        self.VC1.diveSiteTextfield.text = self.userDefault.stringForKey("diveSite")
        self.VC1.diveDurationTextfield.text = self.userDefault.stringForKey("diveDuration")
        self.VC1.diveMaxDepthTextfield.text = self.userDefault.stringForKey("diveDepth")
 ...
        self.DNo.textView.text = self.userDefault.objectForKey("diveNotes") as? String;
        self.userDefault.synchronize()


        self.performSegueWithIdentifier("segueNewDive1", sender: self)

任何帮助,了解为什么会非常感谢nil。

更新了Vladian的评论

0 个答案:

没有答案