gpt4 book ai didi

swift - 在 NSUserDefault 中保存 TextView 时出错

转载 作者:行者123 更新时间:2023-11-30 14:12:49 25 4
gpt4 key购买 nike

how it looks like

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

示例

第一个 TextView - 正确保存

第二个 TextView - 正确保存

第三个 TextView - 正确保存

第四个 TextView - 正确保存

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

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

保存按钮。

看起来所有代码都正常,所有 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)

}

}

最佳答案

我想在这部分:

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 放入最后两行。

关于swift - 在 NSUserDefault 中保存 TextView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31568801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com