Calling specific button made from Loop

I have made a table of buttons that looks like this:

local t = {"one", "two", "three"} local buttons = {} for i = 1, #t do buttons[#buttons+1] = widget.newButton { id = #buttons, label = t[i], height = 70, width = 70, onRelease = btnHandle } buttons[i].x = (i\*30) + (i\*50) buttons[i].y = 300 end

In my btnHandle function for when it is released, how do I call specific buttons to do different functions? Example: If the first button is pressed, it should print “correct” but the second and third button should print “wrong”.

I can get it to print if a button was pressed at all, but cannot figure out how to differentiate between which button was pressed. Would it just be easier to create separate buttons with different onRelease functions? 

That is what you have the id for.

Set id = i

Then in your handle, check event.target.id and react accordingly

Cheers,

Tommy

Hi,

I used your original code and added the function btnHandle and it worked.

Here is the code with some smaller modifications:

local widget = require("widget") local t = {[1] = "one", [2] = "two", [3] = "three"} local buttons = {} function btnHandle(event) print(event.target.id) end for i = 1, #t do buttons[i] = widget.newButton { id = i, label = t[i], height = 70, width = 70, onRelease = btnHandle } buttons[i].x = (i\*30) + (i\*50) buttons[i].y = 300 end

Best regards,

Tomas

It does work!  It’s frustrating sometimes for me to think I am progressing my learning of lua in Corona, but then forget simple things like event.target.  Thanks for all the help!

That is what you have the id for.

Set id = i

Then in your handle, check event.target.id and react accordingly

Cheers,

Tommy

Hi,

I used your original code and added the function btnHandle and it worked.

Here is the code with some smaller modifications:

local widget = require("widget") local t = {[1] = "one", [2] = "two", [3] = "three"} local buttons = {} function btnHandle(event) print(event.target.id) end for i = 1, #t do buttons[i] = widget.newButton { id = i, label = t[i], height = 70, width = 70, onRelease = btnHandle } buttons[i].x = (i\*30) + (i\*50) buttons[i].y = 300 end

Best regards,

Tomas

It does work!  It’s frustrating sometimes for me to think I am progressing my learning of lua in Corona, but then forget simple things like event.target.  Thanks for all the help!