On Jul 28, 2009, at 11:56 PM, Rippit the Ogg Frog wrote:
How can one identify which of several NSTextViews has a given piece of text in it?

I have a window with six NSTextViews. I made the window's controller the delegate for each of them - or rather for their NSText base class. My delegate's -textDidChange method gets called whenever the user edits any of the views, and I can get the text like this:

- (void) textDidChange: (NSNotification*) notification
{
        NSString *text = [[notification object] string];
        
        // But which view was it?

        return;
}

As others have pointed out, [notification object] *is* the text view in question. This is a general pattern -- delegate methods are passed the thing they are a delegate for, and this is one reason. *But* this is not the delegate method you want to implement...

In the list archives I saw a mention of creating a separate outlet in the controller for each NSTextView, with each view being connected to a separate outlet. I think that would work fine for me but I don't quite understand how to go about it - I haven't done much Cocoa programming before.

In the header of your window controller class, declare instance variables like this:

    IBOutlet NSTextView * textViewOne;
    IBOutlet NSTextView * textViewTwo;
    ... etc. ...

Save the file and return to Interface Builder. The new outlets will show up and you'll be able to make connections from your window controller to the various text views.

==> NOTE: make sure to connect to the text views and not the scroll views that enclose them.

My objective is just to capture the text from each view when the window is dismissed, so there could be another way to do what I want without using a delegate. It's not necessary for me to get called for every character the user types.

Implement the window delegate method windowWillClose:. Your window controller is probably already the window's delegate, but if it isn't, make that connection in IB. Then in windowWillClose:, collect the data from your six text views using [textViewOne string], [textViewTwo string], etc.

--Andy


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to