存储UITextView字符串/文本数据

时间:2016-07-22 14:34:16

标签: ios swift swift3 userdefaults

所以我试图创建一个显示可编辑的大文本视图的视图。在故事板中,它被编辑以显示文本“在此处记笔记(向下滑动以关闭键盘):”作为占位符。

我希望它记住当用户编辑文本而不是从那个文本...所以我使用!做了一个if语句。=比较器用于比较swift中的字符串。我不确定它是否会获得应用商店评论的批准,因为他们建议使用isEqualToString()。

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

import Foundation
import UIKit

class 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 openIn
class 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'” - 编译器突出显示行if let newText行,UserDefaults.standardUserDefaults()用红色下划线标出。

 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
    }
}
}

1 个答案:

答案 0 :(得分:0)

您需要在某处保存更改,我建议使用UserDefaults,这是保存用户首选项和其他小块数据的好方法。

我会在viewWillDisappear中替换它:

placeholder = messageNotes.text

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

viewWillAppear我添加:

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