所以我正在尝试创建一个显示可编辑的大文本视图的视图。在情节提要中,它被编辑为显示文本“在此处记笔记(向下滑动以关闭键盘):”作为占位符。
我希望它记住用户何时编辑除此之外的文本......所以我使用 != 做了一个 if 语句,该比较器适用于快速比较字符串。我不确定它是否会得到应用商店评论的批准,因为他们推荐 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
}
}
}