Need some help generalizing my buttons as editable via native.newTextField

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.

it isn’t clear what “button1Data” does, as you seem to assign it a value for no reason, however for the “relate text field to button” aspect of your question (and to whatever extent you can also apply it to whatever “button1Data” is)…

in general, what you probably need to do is give “defaultField” some references to the “things” it needs to work with, so that IT can resolve them for itself.  for example, when creating it in the button’s event::

– event.target is the button object that caused this event, so “memorize” it inside your text field

defaultField.myButton = event.target

then in the text field’s event, refer to IT’S own button like so:

– event.target in this case is the text field

event.target.myButton:setLabel(event.target.text)

(i’d also get rid of that “defaultField” variable as its not needed, and will be just another variable you’d have to “duplicate” for your dozens of buttons, or put them all in a list by key, or don’t store them at all and just remove the text field via the event.target reference)

hth

Try something like this:

local buttons = {} -- store buttons in this table referenced by a string index local data = {} -- Seems you're storing data? Store it here, using the same indexes used for buttons local function onUserInput( self, event ) if (event.phase == "began") then print("gonna edit the button!") elseif (event.phase=="ended" or event.phase=="submitted") then data[self.myIndex] = self.text buttons[self.myIndex]:setLabel( self.text ) display.remove(self) end end local field1 = native.newTextField( ... ) field1.userInput = onUserInput field1.myIndex = "someIndexName" field1:addEventListener( "userInput" ) local field2 = native.newTextField( ... ) field2.userInput = onUserInput field2.myIndex = "someOtherIndexName" field2:addEventListener( "userInput" ) ...

See, this is why I shouldn’t be tackling these things at 2 in the morning!

Of course the solution is to pass the information via some other table, instead of trying to overload the event listener. As both of you pointed out.

FWIW, here’s my final implementation, which works great.

local defaultField ----------------------------------------------------------------------------------------- function editTextFieldListener(event) if (event.phase == "began") then print("gonna edit "..defaultField.buttonName) end if (event.phase=="ended" or event.phase=="submitted") then \_G[defaultField.varName] = event.target.text \_G[defaultField.buttonName]:setLabel(event.target.text) defaultField.varname = nil defaultField.buttonName = nil display.remove(defaultField) end end ----------------------------------------------------------------------------------------- function editTextField(event, fieldX, fieldY, fieldW, fieldH, varName, buttonName) defaultField = native.newTextField(fieldX, fieldY, fieldW, fieldH) defaultField.varName = varName defaultField.buttonName = buttonName defaultField:addEventListener("userInput", editTextFieldListener) end -----------------------------------------------------------------------------------------

This allows me to create widget buttons, which create their own edit field when pressed, and update their data when closed.

(Remember that in my app a “button” is really a visual representation of a data field.)

it isn’t clear what “button1Data” does, as you seem to assign it a value for no reason, however for the “relate text field to button” aspect of your question (and to whatever extent you can also apply it to whatever “button1Data” is)…

in general, what you probably need to do is give “defaultField” some references to the “things” it needs to work with, so that IT can resolve them for itself.  for example, when creating it in the button’s event::

– event.target is the button object that caused this event, so “memorize” it inside your text field

defaultField.myButton = event.target

then in the text field’s event, refer to IT’S own button like so:

– event.target in this case is the text field

event.target.myButton:setLabel(event.target.text)

(i’d also get rid of that “defaultField” variable as its not needed, and will be just another variable you’d have to “duplicate” for your dozens of buttons, or put them all in a list by key, or don’t store them at all and just remove the text field via the event.target reference)

hth

Try something like this:

local buttons = {} -- store buttons in this table referenced by a string index local data = {} -- Seems you're storing data? Store it here, using the same indexes used for buttons local function onUserInput( self, event ) if (event.phase == "began") then print("gonna edit the button!") elseif (event.phase=="ended" or event.phase=="submitted") then data[self.myIndex] = self.text buttons[self.myIndex]:setLabel( self.text ) display.remove(self) end end local field1 = native.newTextField( ... ) field1.userInput = onUserInput field1.myIndex = "someIndexName" field1:addEventListener( "userInput" ) local field2 = native.newTextField( ... ) field2.userInput = onUserInput field2.myIndex = "someOtherIndexName" field2:addEventListener( "userInput" ) ...

See, this is why I shouldn’t be tackling these things at 2 in the morning!

Of course the solution is to pass the information via some other table, instead of trying to overload the event listener. As both of you pointed out.

FWIW, here’s my final implementation, which works great.

local defaultField ----------------------------------------------------------------------------------------- function editTextFieldListener(event) if (event.phase == "began") then print("gonna edit "..defaultField.buttonName) end if (event.phase=="ended" or event.phase=="submitted") then \_G[defaultField.varName] = event.target.text \_G[defaultField.buttonName]:setLabel(event.target.text) defaultField.varname = nil defaultField.buttonName = nil display.remove(defaultField) end end ----------------------------------------------------------------------------------------- function editTextField(event, fieldX, fieldY, fieldW, fieldH, varName, buttonName) defaultField = native.newTextField(fieldX, fieldY, fieldW, fieldH) defaultField.varName = varName defaultField.buttonName = buttonName defaultField:addEventListener("userInput", editTextFieldListener) end -----------------------------------------------------------------------------------------

This allows me to create widget buttons, which create their own edit field when pressed, and update their data when closed.

(Remember that in my app a “button” is really a visual representation of a data field.)