8

I've been trying to learn to use Xcode, but I'm getting confused with how to register that NSTextField has changed. Essentially, I have an NSTextField and a button. Clicking the button does stuff with the text in the field. However, I want to be able to get the text of the field without needing to use the text field "Action:send on end editing." That is, I want to be able to enter text and immediately press the button, without hitting enter or tabbing out of the text box. It seems like the way to do this would be by setting a delegate for my NSTextField that responds to

- (void)controlTextDidChange:(NSNotification *)aNotification 

But I don't understand how to get the text that has been entered. I assume it has something to do with

[[aNotification userInfo] valueForKey:@"NSFieldEditor"]; 

but I really have no idea where to go from there.

3 Answers 3

11

You're on the right track! The object that you get out of the notification's user info dictionary is the Field Editor, which is simply an NSTextView that's handling the text input on the text field's behalf.

Once you have that object, all you have to do is ask it for its textStorage, which is an NSTextStorage* object holding the text. That object, in turn, has its string which is a plain old NSString holding just the characters.

NSTextView * fieldEditor = [[aNotification userInfo] objectForKey:@"NSFieldEditor"]; NSString * theString = [[fieldEditor textStorage] string]; 

*A subclass of NSAttributedString, which is an object holding a string and associated "attributes" like color, font, and underlining.

Sign up to request clarification or add additional context in comments.

2 Comments

May I suggest -objectForKey: instead of -valueForKey:? The former is the canonical method to obtain a value from a dictionary.
@Bavarious: You are right as usual! Guilty here of having copy-pasted the OP's line of code without reading it carefully.
2

In your button action method, simply read the current string value in the text field:

- (IBAction)didClickTheButton:(id)sender { NSString* theString = [myTextField stringValue]; // do something with theString } 

4 Comments

Wow, that is much easier. Why is the other answer, which seems so convoluted, currently rated higher?
@SilverSideDown At least when I asked the question, this will not register text that has been entered without the user tabbing out, or hitting return.
If you're using data bindings you can set the "Continuously Updates Value" option and that will update the bound value as you type without needing to hit return or tab. Calling the -stringValue method will return the current text in the NSTextField regardless.
Darren's answer is the best, and actually requires no coding - except declaring an ivar. Works without issues in 2016 with Swift! ;) Thanks, mate!
2

If you're only ever handling a single text field, this may be simpler:

- (void)controlTextDidChange:(NSNotification *)obj { [self.inputField stringValue]; } 

I'm totally ignoring all the complicated details of NSText and whatnot and just using the simplicity of the notification being sent and the simplicity of getting the string value from a text field.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.