I’m making a simple in-house app which is meant to:
1 - Load data from a file.
2 - Display that data as a series of buttons.
3 - Allow me to tap on a button to edit that piece of data.
4 - Save the edited data file when I’m done making changes.
The good news is - I’ve got it all working! Yay!
But there’s one bit that seems really inefficient to me, which is the fact that I have to create a unique event listener for each native text field instance - just to be certain that the entered data is then assigned correctly as the text field is destroyed. I feel like there should be some way to create a general “open a text field from pressing button A, then submit the string from that text field back to button A” thing.
Here’s what I have that is working:
local defaultField local function editedButton1( event ) if (event.phase == "began") then print("gonna edit the button!") end if (event.phase=="ended" or event.phase=="submitted") then button1Data = event.target.text Button1:setLabel(event.target.text) display.remove(defaultField) end end local function pushButton1( event ) if ("ended" == event.phase) then defaultField = native.newTextField( 500, 300, 400, 70 ) defaultField:addEventListener("userInput", editedButton1 ) end end
I have dozens of functions exactly like editedButton1 - the only difference is the string I use for “button1Data” and “Button1.” I wish I could pass those along into a generic event listener… but I can’t seem to make that work. Anyone know how I can accomplish my goals here?
I think my problem is that I don’t quite understand how the function “editedButton1” gets called… which probably means I don’t understand the guts of eventlisteners.