widget.newButton can't be removed

I have a button like this:

local function myHandler(event)  
 display.remove(myButton)  
 goNextScreen()  
end  
  
local myButton = widget.newButton {  
 x = 40,  
 y = 380,  
 width = 200,  
 label = "Go next screen!",  
 onRelease = myHandler  
 }  
  

My button never gets removed. At first sight that is because you ask the button’s handler to remove the button that references it, but this problem will always exist (e.g. also if you remove it in the next screen).

Any clues? I’ve tried closures with the handler and the buttons and their functions in a function “makeButtons” as well.

Any thoughts greatly appreciated - this should be easy. What am I missing?
[import]uid: 5822 topic_id: 16010 reply_id: 316010[/import]

Hi,

maybe something like this can help, but it just a TRY.

[lua]myButton:removeSelf()[/lua]

[import]uid: 89165 topic_id: 16010 reply_id: 59379[/import]

Hey maartin,

I understand what you said. Glad you got the “first” solution (that is make it works at all) but I see your concernings.

Would be possible to set your variable “myButton” to nil instead trying to hard remove it or if you do not want to set its “nil” maybe hide it? Sorry, maybe am completely wrong but for a try who knows. :]

Like this:
[lua]myButton = nil[/lua]

or hide

[lua]myButton.alpha = 0[/lua]
Regards, [import]uid: 89165 topic_id: 16010 reply_id: 59383[/import]

Tried that first. As I use little resources, I just keep them, put them in a group and use slideRight on the group.

But at some point, you’ll run out of memory with this approach (and luckily I found the slide* functions on groups in the examples). [import]uid: 5822 topic_id: 16010 reply_id: 59380[/import]

Just to be clear: it does NOT work. Setting it to nil won’t remove it from the display. [import]uid: 5822 topic_id: 16010 reply_id: 59385[/import]