ios – 存储UITextView字符串文本数据

ios – 存储UITextView字符串文本数据,第1张

概述所以我试图创建一个显示可编辑的大文本视图的视图.在故事板中,它被编辑以显示文本“在此处记笔记(向下滑动以关闭键盘):”作为占位符. 我希望它记住当用户编辑文本而不是那个…所以我使用了!=该比较器用于比较swift中的字符串.我不确定它是否会得到应用商店评论的批准,因为他们推荐使用isEqualToString(). 无论如何,下面的代码是针对该视图的…如果用户从占位符更改了文本,则不记得输入的文本 所以我试图创建一个显示可编辑的大文本视图的视图.在故事板中,它被编辑以显示文本“在此处记笔记(向下滑动以关闭键盘):”作为占位符.

我希望它记住当用户编辑文本而不是那个…所以我使用了!=该比较器用于比较swift中的字符串.我不确定它是否会得到应用商店评论的批准,因为他们推荐使用isEqualToString().

无论如何,下面的代码是针对该视图的…如果用户从占位符更改了文本,则不记得输入的文本.它识别它已被更改,但当您返回到视图时,它再次显示默认占位符…帮助?

import Foundationimport UIKitclass MessageNotesVIEwController: UIVIEwController,UITextVIEwDelegate {@IBOutlet weak var messageNotes: UITextVIEw!@IBOutlet var swiped: UISwipeGestureRecognizer!var placeholder = "Take your notes here (Swipe Down to dismiss the keyboard): "overrIDe func vIEwWillAppear(_ animated: Bool) {    super.vIEwWillAppear(true)    messageNotes.delegate = self    messageNotes.text = placeholder}overrIDe func dIDReceiveMemoryWarning() {    super.dIDReceiveMemoryWarning()}@IBAction func openIn(_ sender: AnyObject) {    print("Share with Social Media or copy to Notes")    //let textToShare = messageNotes.attributedText       // let objectsToShare = [textToShare]        //let activityVC = UIActivityVIEwController(activityItems: objectsToShare,applicationActivitIEs: nil)    //activityVC.popoverPresentationController?.sourceVIEw = (sender as! UIVIEw)    //self.present(activityVC,animated: true,completion: nil)    if let notes = messageNotes.text {        let avc = UIActivityVIEwController.init(activityItems: [notes],applicationActivitIEs: nil)        avc.popoverPresentationController?.barbuttonItem = sender as? UIbarbuttonItem        self.present(avc,animated: true) {            print("completed")        }    }}func touchesBegan(_ messageNotes: UITextFIEld) -> Bool {    self.vIEw.endEditing(true)    return false}//let defaults = NSUserDefaults.overrIDe func vIEwWilldisappear(_ animated: Bool) {    //PrepareForSegue dIDn't quite work    print("VIEw Will disappear")    //Testing if the user has entered any text if so preserve entered text    if messageNotes.text != placeholder {        //vIEwDIDLoad(typedText)        print("the user altered the text in some way. \n")        placeholder = messageNotes.text        // assigns new placeholder    }    else{        // do something else - if needed    }}}//Sub class decleration for ActivityForNotesVIEwController as called in openInclass ActivityForNotesVIEwController: UIActivityVIEwController {internal func _shouldExcludeActivityType(_ activity: UIActivity) -> Bool {    let activityTypestoExclude = [    ]    if let actType = activity.activityType() {        if activityTypestoExclude.contains(actType) {            return true        }        else if super.excludedActivityTypes != nil {            return super.excludedActivityTypes!.contains(actType)        }    }    return false}}

尝试实现以下答案时存在一些编译错误.

在vIEwWillAppear函数中,编译器说“不能调用非函数类型的值’UserDefaults’” – 如果让带有UserDefaults.standardUserDefaults()的newText行用红色下划线,编译器会突出显示该行.

overrIDe func vIEwWillAppear(_ animated: Bool) {    super.vIEwWillAppear(true)    messageNotes.delegate = self    messageNotes.text = placeholder    if let newText = UserDefaults.standardUserDefaults().objectForKey("textKey"){        if newText.length > 0 {            messageNotes.text = newText        }    }}

在vIEwWilldisappear函数中找到相同的编译错误:

overrIDe func vIEwWilldisappear(_ animated: Bool) {    //Prepare for segue dIDn't quite work for what I need    print("VIEw Will disappear")    //Testing if the user has entered any text if so preserve entered text    if messageNotes.text != placeholder {        //vIEwDIDLoad(typedText)        print("the user altered the text in some way. \n")        UserDefaults.standardUserDefaults().setobject(messageNotes.text!,forKey:"textKey")        // assigns new placeholder    }    else{        // do something else - if needed    }}}
解决方法 您需要在某处保存更改,我建议使用UserDefaults,这是保存用户首选项和其他小块数据的好方法.

我将在vIEwWilldisappear中替换它:

placeholder = messageNotes.text

UserDefaults.standardUserDefaults().set(messageNotes.text!,forKey:"textKey")

在vIEwWillAppear中我添加:

messageNotes.text = placeholderif let newText = UserDefaults.standardUserDefaults().object("textKey"){    if newText.length > 0 {        messageNotes.text = newText as! String    }}
总结

以上是内存溢出为你收集整理的ios – 存储UITextView字符串/文本数据全部内容,希望文章能够帮你解决ios – 存储UITextView字符串/文本数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1003018.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存