Re: How to Change a button state when textFields become not empty
Riccardo Mottola <[email protected]> Thu, 26 Feb 2026 22:06:48 +0100
| Newsgroups | gmane.comp.lib.gnustep.general |
|---|---|
| Message-ID | <[email protected]> |
Patrick CARDONA wrote:
> Hello,
>
> I am working on an Internet Shortcuts Manager (aka SaveLink).
>
> I associated the same action (statechange:) on my two fields ('name'
> and 'link').
> If they are not empty, the 'savebutton' should be enabled.
> But my button state does never change. Initial state is disabled and
> it remains in this state.
>
I didn't download your project, just reading the code.
Are you sure you get statechange called? is it connected?
Put an NSLog(@"statechange called") and check.
>
> In the class:
>
>> @implementation
>> Â // (...)
>>
>> - (IBAction) statechange: (id)sender
>> {
>> Â // We retrieve values from textFileds outlets
>> Â NSString *nameLink = [name stringValue];
>> Â NSString *urlLink = [link stringValue];
>> Â int lenName = [nameLink length];
>> Â int lenLink = [urlLink length];
These are unsigned, or better NSUinteger, not int. Won't change, just
nitpick.
>> Â if(lenName>0 && lenLink>0)
>> Â {
Put an NSLog(@"Enable"); here so you are sure that the condition is
matched and the issue is in setEnabled.
You can easily write this condition as:
if([nameLink length] && [urlLink length])
or for extra paranoids
if((nameLink && [nameLink length]) && (urlLink && [urlLink length]))
It checks that both string are valid and longer than zero. Not
necessary, but it can also be quicker, since it will bail out on a nil
string.
you could also try to trim spaces.
Riccardo