Re: How to Change a button state when textFields become not empty: fixed

Patrick CARDONA <[email protected]> Sat, 28 Feb 2026 18:22:46 +0100
Newsgroups gmane.comp.lib.gnustep.general
Message-ID <65cf9ed205c4703b3b35475cd1db7f21@pi500>
Hi Josh,
Hi Nikolaus,

On 2026-02-27 22:07:06 +0100 Josh Freeman 
<[email protected]> wrote:

> Hi Patrick,
> 
>     The SaveLink instance is being set up as an observer for 
> NSControlTextDidChangeNotifications from within -[SaveLink observe:], 
> but 
> that method isn't called anywhere, except as the textfields' IBAction:
> 
> - Notifications won't be observed until one of the textfields sends 
> its 
> action (user presses <Enter> while the textfield's active)
> 
> - The SaveLink instance will be re-registered for the notifications 
> each time 
> the textfields send the action
> 
>     I suggest moving the -[NSNotificationCenter addObserver:...] 
> calls to 
> -awakeFromNib; That gets called only once, automatically, after 
> loading the 
> .gorm file:
> 
> <<-
> - (IBAction) observe:(id)sender
> {
> // Register for notifications
>    [[NSNotificationCenter defaultCenter] addObserver:self
>  
> selector:@selector(textDidChange:)
>  
> name:NSControlTextDidChangeNotification
>                                                 object:name];
>                                                   
> [[NSNotificationCenter 
> defaultCenter] addObserver:self
>  
> selector:@selector(textDidChange:)
>  
> name:NSControlTextDidChangeNotification
>                                                 object:link];
> }
> 
> ->>
> - (IBAction) observe:(id)sender
> {
> }
> 
> - (void) awakeFromNib
> {
>    // Register for notifications
>    [[NSNotificationCenter defaultCenter] addObserver:self
>  
> selector:@selector(textDidChange:)
>  
> name:NSControlTextDidChangeNotification
>                                                 object:name];
> 
>    [[NSNotificationCenter defaultCenter] addObserver:self
>  
> selector:@selector(textDidChange:)
>  
> name:NSControlTextDidChangeNotification
>                                                 object:link];
> }

I changed then moved '- (void) awakeFromNib' method from 
'AppController' to 'SaveLink' following the above code change.

> 
>     Also, in -[SaveLink textDidChange:]:
> 
> - Calling [[urlLink substringToIndex: 4] isEqualToString: @"http"] 
> will throw 
> an exception if urlLink's length is less than 4, so suggest replacing 
> it with 
> [urlLink hasPrefix: @"http"]

I added this to my Devel memo: great to know.

> 
> - Nesting the urlLink check within the nameLink check can leave the 
> savebutton enabled in cases where the nameLink has non-zero length 
> but the 
> urlLink has no http prefix, so suggest un-nesting the checks:
> 
> <<-
> - (void)textDidChange:(NSNotification *)notification
> {
> ...
>    if([nameLink length])
>    {
>      if([[urlLink substringToIndex: 4] isEqualToString: @"http"])
>      {
>        NSLog(@"Button save enabled");
>        [savebutton setEnabled:YES];
>      }
>    }
>    else
>    {
>      [savebutton setEnabled:NO];
>    }
> }
> 
> ->>
> - (void)textDidChange:(NSNotification *)notification
> {
> ...
>    if([nameLink length] && [urlLink hasPrefix: @"http"])
>    {
>      NSLog(@"Button save enabled");
>      [savebutton setEnabled:YES];
>    }
>    else
>    {
>      [savebutton setEnabled:NO];
>    }
> }
> 

Thank you to point this.
Finally, I used the quick and elegant statement suggested by Nikolaus.

>     Lastly, in SaveLink_main.m's main() function:
> 
> int
> main(int argc, const char *argv[])
> {
>    [NSApp setDelegate: [SaveLink new]];
>    return NSApplicationMain (argc, argv);
> }
> 
>     I suggest removing the call to [NSApp setDelegate: [SaveLink 
> new]]:
> 
> - NSApp isn't valid until NSApplication is initialized, within 
> NSApplicationMain()
> 
> - A SaveLink object will be instantiated when loading the .gorm file 
> (and its 
> IBOutlets & IBActions will be connected correctly), so calling 
> [SaveLink new] 
> will just allocate (& leak) a different SaveLink instance, one 
> without its 
> outlets & actions connected

I better understand all these interactions now.

> 
>     Hope this is helpful!
> 
> Cheers,
> 
> Josh

(...)

Of course, it was. Thank you both, Josh and Nikolaus, for the master 
class.

Now, SaveLink is working as expected and it will be published in the 
next release of AGNoStep.

Cheers,
Patrick

-- 
Patrick Cardona - Pi500 - GNU/Linux aarch64 (Debian 13.3)
Xorg (1:7.7+24) - libcairo2 (1.18.4-1+rpt1 arm64) - Window Maker 
(0.96.0-4)
GWorkspace (1.1.0 - 02 2025) - Theme: AGNOSTEP - Classic - MUA: 
GNUMail (1.4.0 - rev.947)