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")
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
}
override func viewWillDisappear(_ animated: Bool) {
print("View Will Disappear")
if messageNotes.text != placeholder {
print("the user altered the text in some way. \n")
placeholder = messageNotes.text
}
else{
}
}
}
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) {
print("View Will Disappear")
if messageNotes.text != placeholder {
print("the user altered the text in some way. \n")
UserDefaults.standardUserDefaults().setObject(messageNotes.text!, forKey:"textKey")
}
else{
}
}
}
source
share