newbie question - How to get back to the button from an onPress event on a ui.newButton

Running on Windows simulator…
[blockcode]
local myButton = ui.newButton {
default = defaultButton,
onEvent = PressHandler,
text = item,
id = item,
size = 16,
emboss = true
}

function PressHandler(event)

– So far the only one attribute I could reference is event.id
– All other reference like event.target, event.text all fail

end
[/blockcode]

Questions

  1. Is there a generic way to find out what attributes are supported in an event?

  2. Is there any way I could get back to the original button from the event?

Thank you for your help in advance.

Cheers
ec0000

[import]uid: 29942 topic_id: 7353 reply_id: 307353[/import]

i can give you a quick fix for this… open up ui.lua and add this line in the newButton function

[lua]button.params = params – add this line at the end of the function
return button[/lua]

then in your event you can just get any of the parameters you passed in

eg if you have
[lua]local button1 = ui.newButton{
default = “buttonRed.png”,
over = “buttonRedOver.png”,
onPress = button1Press,
onRelease = button1Release,
text = “Button 1 Label”,
emboss = true,
mything=“I am button 1”[/lua]

in your listener you can have

[lua]local button1Press = function( event )
local b1 = event.target
print(“text=”…(b1.params.text))
print(“mything=”…(b1.params.mything))
end[/lua] [import]uid: 6645 topic_id: 7353 reply_id: 25926[/import]

Thanks, jmp909.

The extension makes sense.

I still have issue accessing the parameters and found that the problem is with event.target.

event.target == nil.

Could this be a bug for the Windows simulator?

Ps. I found a workaround. Instead of using the onEvent property in the ui.newButton, I add the event using addEventListener.

[blockcode]
myButton:addEventListener(“touch”, buttonHandler)
[/blockcode]

This works if I use “touch” as the event name.

Then event.target and the newly added params are visible inside the event handler.
[import]uid: 29942 topic_id: 7353 reply_id: 25997[/import]

my example worked fine for me in windows simulator. i was testing it by modifying the “Interface/ButtonEvents” sample in the included Corona demos, but I am using onPress not onEvent
[import]uid: 6645 topic_id: 7353 reply_id: 26037[/import]

You are right. I updated the sample with your changes. It is working.

I would need to find the real reason that it did not work on the main code.

Thanks. [import]uid: 29942 topic_id: 7353 reply_id: 26054[/import]

Just for someone who found this threat later…

The problem of assessing “self” in onEvent, onPress, onRelease with the standard ui.lua is really that when the onTouch event is fired, it then checks for the existence of the other event type and calls the other event type by passing part of the parameters BUT not the self object.

The way I fixed it is to change the ui.lua module so it passes on the self object.

[blockcode]
– note the extra self parameter
local function newButtonHandler( self, event )

if onEvent then
– note the extra self parameter
result = onEvent( self, event )
end
[/blockcode]

For my purpose, I also deleted the onPress and onRelease events as I found them redundant.

[import]uid: 29942 topic_id: 7353 reply_id: 26891[/import]