6

I'm trying to have my text field resign first responder when the user is done editing it, like when they hit enter. The delegate method is getting called but what I have now doesn't work, what is the proper way to go about doing this?

- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
{
    [fieldEditor resignFirstResponder];
    return YES;
}
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
avizzini
  • 789
  • 1
  • 7
  • 17

4 Answers4

28

From the docs on the -resignFirstResponder method:

Use the NSWindow makeFirstResponder: method, not this method, to make an object the first responder. Never invoke this method directly.

This code should do it:

[myWindow makeFirstResponder:nil];
Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • It's causing crash but I think it's another problem, when I do fix it I think your solution will work. Thank You. – avizzini Dec 13 '10 at 17:59
  • The problem is definitely elsewhere. This is the proper way to cause *nothing* to be a window's first responder. – Joshua Nozzi Dec 13 '10 at 19:17
  • When I call this, it causes an infinite loop where the window asks the control control:textShouldEndEditing:, which calls makeFirstResponder: ad infinitum. – SG1 Mar 12 '13 at 16:30
  • @SG1: Post your code into a new question. There is nowhere near enough information in your comment to even guess. – Joshua Nozzi Mar 13 '13 at 14:35
7
- (void)controlTextDidEndEditing:(NSNotification *)obj {

   [self.window makeFirstResponder:view];
}

where "view" is the parent NSView of your NSTextView control.

Kibernetik
  • 2,947
  • 28
  • 35
0

I believe you are looking for the NSControl delegate method:

- (void)controlTextDidEndEditing:(NSNotification *)notif

Implement this method in your NSTextField delegate (easily set in IB).

Then your request to change the responder, such as

[self.window selectNextKeyView:self]

should work.

falinsky
  • 7,229
  • 3
  • 32
  • 56
-4

I am no expert in iPhone programming yet but the method you need to implement is "textFieldShouldReturn" implemented like this:

  - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];
    return YES;
}
  • 1
    Maybe I should have been more clear, Its not for an iPhone application. The method you showed is for UITextField delegate, which is for Cocoa Touch. – avizzini Dec 13 '10 at 16:48
  • This not in osx sdk the every class begins with UI belongs to iOS not OSX – damithH Jan 31 '14 at 05:22