Toggling whether a button is enabled

Can you interrogate a widget button to find out what its current enabled status is?  I was planning to use this in a “toggle enabled” function.  So get the current status of a widget button and if it’s true setEnabled to false, and vice versa.

However just asking for button.isEnabled returns nil.  Any ideas?

I think I’ve worked it out.  The “isEnabled” info is buried in button._view._isEnabled - just where you’d expect to find it!?

Hi @epicurus101,

Sure, that’s a valid way, but if it’s more convenient or easier to read (in code), you could just set your own true/false property on the button, i.e. “.buttonIsEnabled” and toggle it each time you use the “:setEnabled()” API.

Best regards,

Brent

Huh, thanks - I’ve had trouble adding custom fields/parameters to a button in the past, but maybe I’m just thinking of the difficulty you have retrieving that info when the event.target is passed to a listener.

Am I right in thinking that if you have a widget button, and you created a custom field for that widget button, then you can’t easily retrieve that from a touch listener?

Thanks anyway, I’ll try your method too.

Hi again,

Well, since the widget button is ultimately a Lua table, you can add any properties you like to it, and then read that property in the listener, assuming you point to that same object within the listener.

Best regards,

Brent

Argh! You are, of course, right.  My mistake was to try to put my custom parameters into the options table when the button is created, which presumably buries my parameter.  But if I just add it directly to [buttonName] e.g. buttonName.isColour = “Red” then I can retrieve it just fine in the listener with event.target.isColour.

Feel like such an idiot!

I think I’ve worked it out.  The “isEnabled” info is buried in button._view._isEnabled - just where you’d expect to find it!?

Hi @epicurus101,

Sure, that’s a valid way, but if it’s more convenient or easier to read (in code), you could just set your own true/false property on the button, i.e. “.buttonIsEnabled” and toggle it each time you use the “:setEnabled()” API.

Best regards,

Brent

Huh, thanks - I’ve had trouble adding custom fields/parameters to a button in the past, but maybe I’m just thinking of the difficulty you have retrieving that info when the event.target is passed to a listener.

Am I right in thinking that if you have a widget button, and you created a custom field for that widget button, then you can’t easily retrieve that from a touch listener?

Thanks anyway, I’ll try your method too.

Hi again,

Well, since the widget button is ultimately a Lua table, you can add any properties you like to it, and then read that property in the listener, assuming you point to that same object within the listener.

Best regards,

Brent

Argh! You are, of course, right.  My mistake was to try to put my custom parameters into the options table when the button is created, which presumably buries my parameter.  But if I just add it directly to [buttonName] e.g. buttonName.isColour = “Red” then I can retrieve it just fine in the listener with event.target.isColour.

Feel like such an idiot!