Create buttons from array

Hello. I am currently trying to port over some Android apps I have to iOS using Corona. I am wondering if someone could point me in the right direction with a couple things. I have an array of strings that I would like to create buttons from. Preferably I would like them to align in 2 columns by however many rows needed. What’s the best way to accomplish this? [import]uid: 132483 topic_id: 23284 reply_id: 323284[/import]

You can use widget.newButton() to create your buttons, and just store a reference to the buttons in a table.

local widget = require "widget"local array ={ "1st Button", "2nd Button", "3rd Button"}local buttons = {}for i=1,#array do buttons[i] = widget.newButton{ id = "button" .. i, label = array[i], default = "buttonimage.png", over = "buttonimage-over.png", width = 100, height = 24 } button[i]:setReferencePoint( display.CenterLeftReferencePoint ) -- place button positioning logic hereend[/code] [import]uid: 52430 topic_id: 23284 reply_id: 93186[/import]

Thanks! Will give it a try [import]uid: 132483 topic_id: 23284 reply_id: 93306[/import]

As for rows and columns I’ve always found using code like this handy.

[code]
local buttons = {}
local x = startXPOS
for column = 1, totalColumns do
buttons[column] = {}
local offsetY = 0
for row = 1, totalRows do
–Create Buttons here
buttons[column][row] = – create button
y = row * offsetY
x = x
end
x = x + OffsetX
end

[/code] [import]uid: 54776 topic_id: 23284 reply_id: 93321[/import]

@Richard - Sorry, I do not understand why there are two “create button” comments in that code. [import]uid: 132483 topic_id: 23284 reply_id: 94197[/import]

@Jon - It’s my fault I didn’t mean to have two comments stating that. You would create the button just once not twice. I copy and pasted from code I had already and forgot to take out a comment.

I don’t see an edit button so it looks like I can’t fix it!

[import]uid: 54776 topic_id: 23284 reply_id: 94244[/import]

ok, so I would put the create button code in the first “create buttons” comment? or the second? [import]uid: 132483 topic_id: 23284 reply_id: 94300[/import]

@jonathan - on the setReferencePoint line I am getting an error: attempt to index global ‘button’ (a nil value). Any ideas? [import]uid: 132483 topic_id: 23284 reply_id: 94303[/import]

error resolved by using:
[lua]buttons[i].view:setReferencePoint( display.TopLeftReferencePoint )[/lua] [import]uid: 132483 topic_id: 23284 reply_id: 94309[/import]

Pretty much you would put your buttons in the array.

  
buttons[column][row] = display.newImage(image)  
buttons[column][row].touch = onPress  
buttons[column][row]:addEventListener("touch", buttons[column][row])  
  

and then of course create the onPress function that handles the button presses

  
function onPress(object, event)  
  
if event.phase=="began" then  
 print("Button Pressed!")  
  
end  
if event.phase=="ended" then  
  
 print("Button Press Ended!")  
end  
  
end  

[import]uid: 54776 topic_id: 23284 reply_id: 94322[/import]

Thanks! [import]uid: 132483 topic_id: 23284 reply_id: 94570[/import]