Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTextView outlet and object's instance variables.

NSTextView outlet and object's instance variables.
Thread Tools
Fresh-Faced Recruit
Join Date: Dec 2002
Status: Offline
Reply With Quote
Aug 3, 2003, 07:01 PM
 
I have an array of objects that I load into a NSTableView. When I click on a row in the tableView, I have it populate a few NSTextFields and a NSTextView with data that aren't displayed in the tableView.

One of the fields that it populates is a NSTextView. When the user add/edits the string in the NSTextView, they can click on a button to update the object.

I step through the code and it seems to update the object.

However I realized that as I steped through the code, the instance variable in the object already has the value that was placed in the NSTextView prior to clicking the "Update" button.

I am under the assumption that NSTextViews work relatively the same as NSTextField in retrieving the string and setting the value to an instance variable of an object.
This doesn't seem to be the case.

Can someone explain to me how NSTextViews work? I'm a beginner trying to learn Cocoa and just making my own apps as I read through Learning Cocoa book.

The Learning Cocoa book made it appear that NSTextView worked the same way as NSTextFields in retrieving and setting instance variables of the value in it.

Not sure if this is of any help, it is a Document based Cococa Application..
I'd appreciate any insight..
     
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status: Offline
Reply With Quote
Aug 3, 2003, 07:14 PM
 
I had a similar problem that was solved when I realized that the NSTextView doesn't replace it's NSString when setString is called, instead it updates it's own NSMutableString. So when you call -string on NSTextView you get an NSMutableString that will keep getting changed to the NSTextView's new value
     
Fresh-Faced Recruit
Join Date: Dec 2002
Status: Offline
Reply With Quote
Aug 3, 2003, 10:13 PM
 
Originally posted by Uncle Skeleton:
I had a similar problem that was solved when I realized that the NSTextView doesn't replace it's NSString when setString is called, instead it updates it's own NSMutableString. So when you call -string on NSTextView you get an NSMutableString that will keep getting changed to the NSTextView's new value
Thanks for the reply Uncle Skeleton.
Just to make sure I understand, if I set the value of the instance variable of an object, it points to the NSMutableString of the NSTextView and if I set another instance variable of another object, the two objects' instance variable point to the same NSMutable String.

My question is, how do I set the value of the instance variable of the object to the value in the NSTextView without pointing to the NSMutableString?

My current code right now:
[currentChart setChartNotes:[notesTextView string]];

[currentChart setChartNotes:[ what do I put in here]?

Thanks...
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Aug 3, 2003, 10:23 PM
 
The best solution:

For NSTextView objects, call setString: as you normally would, no biggie. When you want to retrieve the string, do this:

NSString *stringVal = [[textView string] copy];

This way you get a copy of the string instead of the NSMutableString that the NSTextView holds onto.

Another good way to do this is to add a category to NSTextView

Code:
@interface NSTextView (MFAdditions) - (NSString *)stringValue; @end @implementation NSTextView (MFAdditions) - (NSString *)stringValue { return [[self string] copy]; } @end
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Aug 3, 2003, 10:47 PM
 
[currentChart setChartNotes:[notesTextView string]];

is fine, but in the setChartNotes: method implementation, you should be using something like:

chartNotes = [sentString copy];

Whereas, it sounds like you're probably using:

chartNotes = [sentString retain];

at the moment. Both are correct, depending on the situation. It sounds like the first one (copy) would suit your situation better than the second one (retain) at athe moment
     
Fresh-Faced Recruit
Join Date: Dec 2002
Status: Offline
Reply With Quote
Aug 3, 2003, 11:49 PM
 
Ghoser777 and Brass thanks for your quick replies.

I tried both of your solutions and both equally worked. I guess I have plenty more to learn.

I do have another question which is something more on the issue of design and differences on using a Document-based Cocoa Application and just a Cocoa Application.

I've been reading "Learning Cocoa" and "Cocoa Programming for Mac OS X" and one thing that I noticed in the very beginning is "Learning Cocoa" starts of using Cocoa Application and creating a controller object for the controller between the View and Model. On the other hand, "Cocoa Programming for Mac OS X" starts of with a Cocoa Document-based Application and using the MyDocument instance as the controller.

How does this theoretically differ besides that Cocoa Document-based Applications, you can have new windows of your application running.

I tried using both approaches on my application that I am working on and I seem to get it to work using a Cocoa Document-based Application but not the Cocoa Application when I created an instance of the controller.

Thanks again....
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:16 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2