I have created a “notes” field designed to hold multiple paragraphs of text which I would like to store in a custom object. Originally, I just used an NSTextField as a temporary solution, but this does not allow me to scroll or have multiple paragraphs of text…
In IB I have placed a NSTextView (which seems to be wrapped inside an NSScrollView.) Upon execution of my program, seems to allow me to enter text in multiple paragraphs, scroll, etc. In short it LOOKS to be exactly what I want would like it to be. So far so good.
Now, I need to retrieve the data from this field and store it in my custom object. This is where I’m getting a bit lost within the developer documentation…
My goals are fairly straight forward:
-
Allow users to type away in the box.
-
Store the contents of the box into a variable (array, etc.) in my custom object when the user moves to another field, leaving the notes field.
-
Display the users stored text in the text box next time the record is viewed.
Second, is there a simple way to retrieve and store the data into a “notes” variable in my custom object (such as an NSString object? I would think having multiple would exclude an NSString object as an option here, but maybe I’m wrong) or am I getting into a more complex area here (such as having to store it in an array of NSString objects, etc.)?
Any help would be appreciated!
You can get the data using
-string, defined by NSText (e.g.NSString *savedString = [aTextView string])Your save code can be put in your
NSTextDelegate(read, delegate of theNSTextView, because it’s the immediate superclass), in– textDidEndEditing:which will be called, well, when editing is finished (e.g. when the user clicks outside the view) or one of the other methods.Then to reload the saved string if you emptied the text view or something, use
[textView setString:savedString]before editing begins.NSTextDelegate documentation: here.
I’m not sure what you mena when you say “store the contents of the box into a variable (array, etc.) Are you hoping for an array of custom notes? Text views store a string of data, so the easiest way of storing its value is using one string; if you need an array of notes you’d have to split the string value into different paragraphs, which shouldn’t be too hard.