Notification on text change for NSTextField

Discussion in 'Mac Programming' started by satyam90, May 29, 2008.

  1. satyam90 macrumors regular


    Jul 30, 2007
    Bangalore, India
    I am using Cocoa with Obj C on Mac Tiger.
    I have a textfield in a panel where path of xml file will be entered. Also I have a button "Browse" which will show the file open dialog for selection of file. When OK clicked, the file name is updated in the textfield. For this purpose I am having the following code in awakeFromNib:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:)  name:NSControlTextDidChangeNotification object:filePath];
    //filePath is the outlet for text field

    In the browse button action, I am having following code:

    - (IBAction)onBrowse:(id)sender
    	int result;
    	NSArray* types = [NSArray arrayWithObject:@"xml"];
    	NSSavePanel *sPanel = [NSSavePanel savePanel];
    	[sPanel setAllowedFileTypes:types];
    	result = [sPanel runModalForDirectory:NSHomeDirectory() file:nil];
    	if (result == NSOKButton)
    		NSString* fName = [NSString stringWithString:[sPanel filename]];
    		[filePath setStringValue:fName];
    The code for textDidChange is as follows:

    - (void)textDidChange:(NSNotification *)aNotification
    	if([aNotification object] == filePath)
               Log(@"User changing the text"); //Log is user defined method to write to a file.

    My problem is that when I am editing I am able to see the message, but when browse button is clicked and selecting the file and setting the string using "setStringValue:", it's not calling the notification function. Why?
  2. adrian.oconnor macrumors 6502

    Jan 16, 2008
    Nottingham, England
    I can't answer your question; I don't know why setting the text directly doesn't invoke the textDidChange notification (or call the controlTextDidChange delegate, if you've created one). It seems odd to me too, but I guess it's by design.

    I'm sure you've already worked around the problem by manually calling your text-changed method at the appropriate time, but there is an alternative: you could use NSControlTextDidEndEditingNotification. To make that work, you would call [self.window makeFirstResponder: self.window] after setting the text (to remove focus from the TextField, and thus calling the notification).
  3. tacoman667 macrumors regular

    Mar 27, 2008
    My guess would be that when you are editing the textview that it fires the action you see through the use of the control itself. When programatically setting the text value, you may need to instantiate a delegate or manually fire the action yourself because the events automated by the control are not being fired. Essentially, the action fires because the control is being interacted with. You are not interacting with the control so the object being "saved" never fires a event.

    Click control > beginedit action > changedtext action > endedit action > commit value to object.

    commit value to object.

    You are skipping the controls events and going straight to the object. I bet if you use a delegate method all the notifications will resolve.

Share This Page