tags:

views:

268

answers:

2

I have created two cocoa controls on my GUI. NSButton and NSTextField. When user clicks on the button, button handler will be executed. It will make button disable and NSTextField enable. On end editing, the string of text field will be shown as a button title.

button click
{
 - Make button visible = false;
 - Make text field visible = true;
 - Text field allow selection = true;
 - text field allow editing = true;
}

control text end editing
{
  - text field allow selection = false;
  - text field allow editing = false;
  - Set button title as text field string;
  - Set text field visible = false;
  - Set button visible = true;
}

I have written above two functions to achieve the functionality. I am getting required functionality when user presses Tab key, as the view switches to next view. But, I want that, once user enters required text in the text field, he can press Enter or click outside of text field to end editing.

On end editing, text field should be closed and text written should be displayed as a title of button.

Please let know, how this functionality can be achieved. Any help would be appreciated.

Thanks in advance...

+1  A: 

Here's an interesting Apple Tech Article on the subject of capturing enter in an NSTextField.

As for loss of focus, look into

-(BOOL)resignFirstResponder

There is some more discussion on this CocoaDev page

nall
A: 

I'd make an object the delegate of your NSTextField and then implement the delegate method:

- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
   - text field allow selection = false;
   - text field allow editing = false;
   - Set button title as text field string;
   - Set text field visible = false;
   - Set button visible = true;
}

I believe this captures both the case where you hit enter and where you tab out.

Michael Bishop