Take the 2-minute tour ×

I am developing an application in XCode 4.6.

To get text-change notifications from NSTextField controls I:

  • Put NSTextField control on window.
  • Connect control delegate to File's Owner via right-click in IB, drag from delegate to File's Owner.
  • Implement controlTextDidChange in window class.

For the application, the window class is my AppDelegate and File's Owner is NSApplication. For the modal dialog, the window class an NSWindowController and File's Owner is of the same type.

If I put a breakpoint in controlTextDidChange, in the AppDelegate class, it never fires. If I do the same procedure with a modal dialog it works fine.

I know in the main application window case the delegate for the control is not my AppDelegate.

What am I doing wrong in hooking up my control delegate in the main window? I must be missing something simple. Is File's Owner the correct delegate to set for controls?

Any help would be appreciated.

Here is some code as requested.

// AppDelegate.h  
// SimpleApplication  

#import <Cocoa/Cocoa.h>  
#import "SimpleTest/SimpleTest.h"  

@interface AppDelegate : NSObject <NSApplicationDelegate>  

@property (assign) IBOutlet NSWindow *window;  

@property (assign) IBOutlet NSTextField *textField;  


// AppDelegate.m  
//  SimpleApplication  

#import "AppDelegate.h"  

@implementation AppDelegate  

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification  
  // Not much to do here for now.  

// Breakpoint set in this function never fires.  
- (void)controlTextDidChange:(NSNotification *)obj  
  NSMutableString* description= [[NSMutableString alloc] init];  
  id aDelegate= [_textField delegate];  
  Class delegateClass= [aDelegate class];  
  [description setString:[delegateClass description]];  
  [description release];  

// To provide some information about the delegates.  
- (IBAction)textChange:(id)sender  
  NSTextField* theTextField= (NSTextField*)sender;  
  NSMutableString* description= [[NSMutableString alloc] init];  
  id aDelegate= [theTextField delegate];  
  Class delegateClass= [aDelegate class];  
  [description setString:[delegateClass description]];  
  [description release];  


Here is a shot of the right-click information for the NSTextField on the main window -

Identity inspector shows File's Owner as NSApplication, which is what I see in the debugger when I put a breakpoint in textChange and hit return in the text field. However, self, the implementor of controlTextDidChange, is AppDelegate. By contrast, in a modal dialog, self and File's Owner are the same object, derived from NSWindowController.

So, the upshot is that I do not have the correct delegate assigned to the control in the main window - how do I do that?

share|improve this question
Doh - my last line there can be reworded as "How do I make App Delegate the delegate for the NSTextField." The answer is just connect them instead of File's Owner! –  GTAE86 Feb 8 '13 at 14:58
add comment

2 Answers

up vote 0 down vote accepted

Oh, wow - in adding more detail to my question, I think I figured out the answer. Instead of dragging from text field delegate to File's Owner, just drag to the blue cube that represents App Delegate!

share|improve this answer
add comment

Can you post some code?

When using delegates make sure you specify that a class implements the required protocol.

@interface MyClass : NSObject <SomeProtocol>

Also make sure you are creating a property to store the delegate.

@property (strong, nonatomic) id<SomeProtocol> delegate;

RE this:

Note that although NSControl defines delegate methods, it does not itself have a delegate. Any subclass that uses these methods must have a delegate and the methods to get and set it. In addition, a formal delegate protocol NSControlTextEditingDelegate Protocol also defines delegate methods used by control delegates. ...

These include: controlTextDidBeginEditing:, controlTextDidChange:, and controlTextDidEndEditing:

share|improve this answer
Added code, and maybe figured out how to make it work. It does work, but is that the correct way to do it? Does that cause any harmful side effects? –  GTAE86 Feb 8 '13 at 14:56
add comment

Your Answer


