在NSUserDefault中保存文本视图时出错

时间:2015-07-22 16:25:10

标签: swift textview

how it looks like

我有6个textViews和一个按钮来保存写在其中的文本。 问题是按钮只保存到第四个TextView,第五个和第六个没有正确保存,他们只是复制第三和第四个文本视图中的文本

实施例

第一个文字视图 - 正确保存

第二个文字视图 - 正确保存

第三个文字视图 - 正确保存

第四个文字视图 - 正确保存

第五个文本视图 - 它是第三个文本视图上的文本的副本

第六个文本视图 - 它是第四个文本视图上的文本的副本

保存按钮。

似乎所有代码都正常,所有文本视图都已成功链接,但最后两个textView仍然失败。

我的代码在这里:

@IBOutlet weak var scrollView: UIScrollView!

@IBOutlet weak var labelUno: UILabel!
@IBOutlet weak var labelDos: UILabel!


@IBOutlet weak var primerTextView: UITextView!

@IBOutlet weak var segundoTextView: UITextView!

@IBOutlet weak var tercerTextView: UITextView!

@IBOutlet weak var cuartoTextView: UITextView!

@IBOutlet weak var quintoTextView: UITextView!

@IBOutlet weak var sextoTextView: UITextView!






override func viewDidLoad() {
    super.viewDidLoad()

      scrollView.contentSize.height = 2000

    labelUno.text = "¿Qué significa el éxito para ti? , ¿significa ser millonario , ser famoso , contribuir de alguna manera a ayudar a muchas personas, ser un buen padre o madre de familia, ser el mejor en tu profesión??"

    primerTextView.layer.borderWidth = 0.8
    primerTextView.layer.borderColor = UIColor.blackColor().CGColor
    primerTextView.layer.cornerRadius = 8.0


    segundoTextView.layer.borderWidth = 0.8
    segundoTextView.layer.borderColor = UIColor.blackColor().CGColor
    segundoTextView.layer.cornerRadius = 8.0

    tercerTextView.layer.borderWidth = 0.8
    tercerTextView.layer.borderColor = UIColor.blackColor().CGColor
    tercerTextView.layer.cornerRadius = 8.0

    cuartoTextView.layer.borderWidth = 0.8
    cuartoTextView.layer.borderColor = UIColor.blackColor().CGColor
    cuartoTextView.layer.cornerRadius = 8.0

    quintoTextView.layer.borderWidth = 0.8
    quintoTextView.layer.borderColor = UIColor.blackColor().CGColor
    quintoTextView.layer.cornerRadius = 8.0

    sextoTextView.layer.borderWidth = 0.8
    sextoTextView.layer.borderColor = UIColor.blackColor().CGColor
    sextoTextView.layer.cornerRadius = 8.0




    //Save Button
    let stringKey = NSUserDefaults.standardUserDefaults()

    segundoTextView.text = stringKey.stringForKey("key")
    primerTextView.text = stringKey.stringForKey("key2")
    tercerTextView.text = stringKey.stringForKey("key3")
    cuartoTextView.text = stringKey.stringForKey("key4")
    quintoTextView.text = stringKey.stringForKey("key5")
    sextoTextView.text = stringKey.stringForKey("key6")

    //Hide keyboard
    //Hide keyboard with touching anywhere
    var tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")
    view.addGestureRecognizer(tap)

}


func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()

    return true
}


@IBAction func saveButton(sender: AnyObject) {



    let myText = segundoTextView.text
    let myText2 = primerTextView.text
    let myText3 = tercerTextView.text
    let myText4 = cuartoTextView.text
    let myText5 = quintoTextView.text
    let myText6 = sextoTextView.text


    NSUserDefaults.standardUserDefaults().setObject(myText, forKey: "key2")
    NSUserDefaults.standardUserDefaults().setObject(myText2, forKey: "key")
    NSUserDefaults.standardUserDefaults().setObject(myText3, forKey: "key3")
    NSUserDefaults.standardUserDefaults().setObject(myText4, forKey: "key4")
    NSUserDefaults.standardUserDefaults().setObject(myText3, forKey: "key5")
    NSUserDefaults.standardUserDefaults().setObject(myText4, forKey: "key6")

    NSUserDefaults.standardUserDefaults().synchronize()

    var alert = UIAlertController(title: "Guardado", message:"Tu texto se ha guardado", preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))

    self.presentViewController(alert, animated: true, completion: nil)


}


//Hide keyboard with return button
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if text == "\n"
    {
        primerTextView.resignFirstResponder()
        segundoTextView.resignFirstResponder()
        tercerTextView.resignFirstResponder()
        cuartoTextView.resignFirstResponder()
        quintoTextView.resignFirstResponder()
        sextoTextView.resignFirstResponder()


        return false
    }
    return true
}


//Hide keyboard with touching anywhere

func DismissKeyboard(){


    view.endEditing(true)

}

}

1 个答案:

答案 0 :(得分:0)

我想在这一部分:

NSUserDefaults.standardUserDefaults().setObject(myText, forKey: "key2")
NSUserDefaults.standardUserDefaults().setObject(myText2, forKey: "key")
NSUserDefaults.standardUserDefaults().setObject(myText3, forKey: "key3")
NSUserDefaults.standardUserDefaults().setObject(myText4, forKey: "key4")
NSUserDefaults.standardUserDefaults().setObject(myText3, forKey: "key5")
NSUserDefaults.standardUserDefaults().setObject(myText4, forKey: "key6")

应该是:

NSUserDefaults.standardUserDefaults().setObject(myText2, forKey: "key2")
NSUserDefaults.standardUserDefaults().setObject(myText, forKey: "key")
NSUserDefaults.standardUserDefaults().setObject(myText3, forKey: "key3")
NSUserDefaults.standardUserDefaults().setObject(myText4, forKey: "key4")
NSUserDefaults.standardUserDefaults().setObject(myText5, forKey: "key5")
NSUserDefaults.standardUserDefaults().setObject(myText6, forKey: "key6")

看起来像是一个复制/粘贴错误:在最后两行放置myText5myText6而不是myText3myText4