Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
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:

Code:
[[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:

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:

Code:
- (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?
 

adrian.oconnor

macrumors 6502
Jan 16, 2008
326
3
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).
 

tacoman667

macrumors regular
Mar 27, 2008
143
0
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.

programatically:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.