input function

I try to implement a simple function to input a variable from the user with this code:

local textBox

local function inputListener( event )

    if event.phase == “ended” then

        return

    elseif event.phase == “editing” then

        print( event.text )

    end

end

textBox = native.newTextField( 15, 70, 280, 70 )

textBox.text = “default text”

textBox:addEventListener( “userInput”, inputListener )

local input = textBox.text

print( "input = " … input )

the code is executed with no pause and print      input = default text

in the simulator i can modify the text but can’t get the ended phase

Programs execute in a linear fashion unless they are told to branch or loop.  There is also a concept called events that can trigger things.  Because of the way Corona SDK works, your code, with no human intervention is going to run from top to bottom and you will get “input = default text” in your output.  You define the text field on the screen, you set up a function to handle the events it generates, but your output comes after the setup.

The native.newTextField() is a complex beast and it’s easy to get tripped up.   When you interact with the field, events happen then your inputListener function executes.  You should probably have a print in the if event.phase == “ended” block to see what your results are.  This is one of the cases where you have to pay close attention to the documentation and sample apps.

During the “editing” phase, event.text exists.  During the “ended” phase it does not.  You have to actually access event.target.text to get the string in the “ended” or “submitted” phase.  This may seem weird but there is rational behind it.

Programs execute in a linear fashion unless they are told to branch or loop.  There is also a concept called events that can trigger things.  Because of the way Corona SDK works, your code, with no human intervention is going to run from top to bottom and you will get “input = default text” in your output.  You define the text field on the screen, you set up a function to handle the events it generates, but your output comes after the setup.

The native.newTextField() is a complex beast and it’s easy to get tripped up.   When you interact with the field, events happen then your inputListener function executes.  You should probably have a print in the if event.phase == “ended” block to see what your results are.  This is one of the cases where you have to pay close attention to the documentation and sample apps.

During the “editing” phase, event.text exists.  During the “ended” phase it does not.  You have to actually access event.target.text to get the string in the “ended” or “submitted” phase.  This may seem weird but there is rational behind it.