Saving String / Text Data UITextView

So, I'm trying to create a view that displays a large text view, editable. In the storyboard, it has been edited to display the text "Take your notes here (" Swipe down to reject the keyboard "):" as a placeholder.

I would like him to remember when a user edits text other than this ... so I made an if statement using! = This comparator works to compare strings in swift. I'm not sure if it will be approved by browsing the app store because they recommend isEqualToString ().

In any case, the code below is intended for this type ... It does not remember the entered text if it is changed by the user from the placeholder. It recognizes that it has been modified, but when you return to the view, it will again display the default placeholder ... Help?

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

When trying to execute the answer below there were some compilation errors.

In the viewWillAppear function, the compiler says: "It is impossible to call a value of the non-functional type" UserDefaults "" - the compiler selects a line if let newTextwith a line UserDefaults.standardUserDefaults()underlined in red.

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

}

The same compilation error is in the viewWillDisappear function:

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
    }
}
}
+4
source share
1 answer

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

Source: https://habr.com/ru/post/1648799/


All Articles