 |
 |
textDidChange: ?
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
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
|
|
try YES and NO instead of TRUE and FALSE
|
3R1C
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2000
Location: Los Altos, CA, USA
Status:
Offline
|
|
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
|
|
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:<nameOfYourTextFieldGoesHere>]
</font>[/code]
do this somewhere during the initialization of your app -- hope that helps
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
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:<nameOfYourTextFieldGoesHere>]</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
|
|
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 ]
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|