Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > textDidChange: ?

textDidChange: ?
Thread Tools
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Mar 30, 2002, 10:30 PM
 
Okay, bear with me, I'm a Cocoa newbie. What I am basically trying to accomplish.. if a text field is empty, the button is disabled, if anything is in it, it's enabled. Simple enough?

Well here's basically what I did...
[list=1][*] Made a sub-class of NSObject called Controller.[*] Added some outlets to my button and text field.[*] Made the Controller a delegate of NSApplication[*] Made Controller a delegate of my text field.[*] Created the files for Controller[*] Added the following to my Controller.h file:
    <BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>- (void)textDidChange NSNotification *)notification;</font>[/code][*] Added the following to my Controller.m file:
    <BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>- (void)textDidChange NSNotification *)notification
{
    NSLog(<font color = orange>@"it worked"</font>);
    if ([[textfield stringValue] isEqualToString:<font color = orange>@""</font>]) {
        [button setEnabled:false];
    }
    else {
        [button setEnabled:true];
    }
}</font>[/code][*] Compiled it, messed around with the text field, it never gets called at all, nothing gets logged and the button never changes its state.[/list=a]

So what can I do? Here is the project if it helps.

Thanks in advance
     
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Mar 31, 2002, 12:20 AM
 
try YES and NO instead of TRUE and FALSE
3R1C
     
Fresh-Faced Recruit
Join Date: Jan 2000
Location: Los Altos, CA, USA
Status: Offline
Reply With Quote
Mar 31, 2002, 01:14 AM
 
or, if you want it to actually work ...

subclass NSTextField and override the textDidChange: method to do what you want it to do (plus you can post a notification so that the nib's controller will handle your notifiaction)

Note, however, this works with NSTextFields- NSTextViews are a different beast. The NSTextView's delegate will take care of the textDidChange: notification.
     
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Mar 31, 2002, 04:30 AM
 
well, you don't need to subclass anything to get this to work... did you register for that notification with the notificationcenter?

the name of your method isn't really important.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange name:NSControlTextDidChangeNotification object:&lt;nameOfYourTextFieldGoesHere&gt;]
</font>[/code]

do this somewhere during the initialization of your app -- hope that helps
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Mar 31, 2002, 02:40 PM
 
Originally posted by seb2:
<STRONG>well, you don't need to subclass anything to get this to work... did you register for that notification with the notificationcenter?

the name of your method isn't really important.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange name:NSControlTextDidChangeNotification object:&lt;nameOfYourTextFieldGoesHere&gt;]</font>[/code]

do this somewhere during the initialization of your app -- hope that helps</STRONG>
Thanks for all of your help, particularly seb... it works!!

3R1C: Actually, my function itself was working fine, it just wasn't getting called at all, I know this because nothing was logged.

I guess there's still a lot I need to learn I am just a bit confused on why some events get called right when you add them while you have to mess with others...
     
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
Mar 31, 2002, 03:57 PM
 
You shouldn't have to register the delegate object as an observer for notifications. It would have worked (I tried it in your program) if you would have just named the method in your controller.h and controller.m files:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier> - (void)controlTextDidChange NSNotification *)aNotification</font>[/code]

For future reference, if you check out the cocoa documentation for NSTextField it says "causes the receiver's delegate to receive a controlTextDidChange: message" under textDidChange. All you have to do is implement controlTextDidChange in your delegate because delegates are automatically registered as notification observers for the methods they implement.

[ 03-31-2002: Message edited by: itai195 ]
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:29 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2