使用 NSUserDefaults 保存来自 UITextView 弹出窗口的击键
Using NSUserDefaults to save keystroke from a UITextView Popover
我有一个名为 NotesViewController
的弹出窗口,其中包含一个 UITextView
。当用户单击 GraphViewController
中的 Notes
按钮时,会出现弹出窗口。
弹出窗口显示正常,但每当我单击弹出窗口并且我希望能够保存它时,UITextField
中的文本就会消失。
如何在每次击键后或用户点击弹出窗口时保存文本的示例也可以作为答案!!
这是我的资料:
class NotesViewController: UIViewController
{
@IBOutlet weak var noteView: UITextView! {
didSet{
defaults.setObject(noteView.text, forKey: "previousText")
noteView.text = notes
}
}
var notes = " " {
didSet {
noteView?.text = notes
}
}
let defaults = NSUserDefaults.standardUserDefaults()
class GraphViewController: UIViewController, GraphViewDataSource, UIPopoverPresentationControllerDelegate
{
//
//...
// prepare segue for popover
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
switch identifier {
//this is the identifier for the popover segue
case "Show Notes":
if let nvc = segue.destinationViewController as? NotesViewController {
if let ppc = nvc.popoverPresentationController {
ppc.delegate = self
}
if let previousNotes = nvc.defaults.stringForKey("previousText") {
nvc.notes = previousNotes
}
}
default: break
}
}
}
}
Rory 的评论是正确的。将 NotesViewController 设置为委托允许我使用跟踪更改的方法。
这是我的解决方案:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//first check if this is the segue we are talking about here
if let identifier = segue.identifier {
switch identifier {
case "Show Notes":
if let nvc = segue.destinationViewController as? NotesViewController {
if let ppc = nvc.popoverPresentationController {
ppc.delegate = self
}
if let previousNotes = nvc.defaults.stringForKey("textPreviouslyEnteredByUser") {
nvc.notes = previousNotes
}
}
default: break
}
}
}
和
class NotesViewController: UIViewController, UITextViewDelegate
{
var defaults = NSUserDefaults.standardUserDefaults()
@IBOutlet weak var noteView: UITextView! {
didSet {
noteView?.text = notes
}
}
override func viewDidLoad() {
super.viewDidLoad()
noteView.delegate = self
}
func textViewDidChange(textView: UITextView) {
defaults.setObject(textView.text, forKey: "textPreviouslyEnteredByUser")
}
var notes = " " {
didSet {
noteView?.text = notes
}
}
}
我有一个名为 NotesViewController
的弹出窗口,其中包含一个 UITextView
。当用户单击 GraphViewController
中的 Notes
按钮时,会出现弹出窗口。
弹出窗口显示正常,但每当我单击弹出窗口并且我希望能够保存它时,UITextField
中的文本就会消失。
如何在每次击键后或用户点击弹出窗口时保存文本的示例也可以作为答案!!
这是我的资料:
class NotesViewController: UIViewController
{
@IBOutlet weak var noteView: UITextView! {
didSet{
defaults.setObject(noteView.text, forKey: "previousText")
noteView.text = notes
}
}
var notes = " " {
didSet {
noteView?.text = notes
}
}
let defaults = NSUserDefaults.standardUserDefaults()
class GraphViewController: UIViewController, GraphViewDataSource, UIPopoverPresentationControllerDelegate
{
//
//...
// prepare segue for popover
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
switch identifier {
//this is the identifier for the popover segue
case "Show Notes":
if let nvc = segue.destinationViewController as? NotesViewController {
if let ppc = nvc.popoverPresentationController {
ppc.delegate = self
}
if let previousNotes = nvc.defaults.stringForKey("previousText") {
nvc.notes = previousNotes
}
}
default: break
}
}
}
}
Rory 的评论是正确的。将 NotesViewController 设置为委托允许我使用跟踪更改的方法。
这是我的解决方案:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//first check if this is the segue we are talking about here
if let identifier = segue.identifier {
switch identifier {
case "Show Notes":
if let nvc = segue.destinationViewController as? NotesViewController {
if let ppc = nvc.popoverPresentationController {
ppc.delegate = self
}
if let previousNotes = nvc.defaults.stringForKey("textPreviouslyEnteredByUser") {
nvc.notes = previousNotes
}
}
default: break
}
}
}
和
class NotesViewController: UIViewController, UITextViewDelegate
{
var defaults = NSUserDefaults.standardUserDefaults()
@IBOutlet weak var noteView: UITextView! {
didSet {
noteView?.text = notes
}
}
override func viewDidLoad() {
super.viewDidLoad()
noteView.delegate = self
}
func textViewDidChange(textView: UITextView) {
defaults.setObject(textView.text, forKey: "textPreviouslyEnteredByUser")
}
var notes = " " {
didSet {
noteView?.text = notes
}
}
}