textField will not disappear

I have the following snippet in my code. As a response to a button tap, I want the user to input his telephone number (8 digits). When done, the textField should disappear (object:removeSelf()), but it doesn’t.

What’s wrong?

local function textListener( event ) if ( event.phase == "editing" ) then if ( string.len(event.text) == 8 ) then telno = event.text print("Nummeret er " .. telno) defaultField:removeSelf() defaultField = nil end end end local function handleIDButtonEvent(event) -- Create text field defaultField = native.newTextField( display.contentCenterX, display.contentCenterY \* 0.05, 100, 30 ) defaultField:addEventListener( "userInput", textListener ) defaultField.inputType = "phone" defaultField.align = "center" end

Does your print("Nummeret er " … telno) line print out?

Are you testing on a device?

  1.  Please use code formatting for future code posts (click the <> symbole when editing a post and paste your code in the popup).

  2. Prefer display.remove( obj ) to obj:removeSelf()  – The prior is safer.

  3. Be sure to clear the keyboard focus with: native.setKeyboardFocus( textField )

    display.remove( defaultField ) defaultField = nil native.setKeyboardFocus( nil )

JonPM: Yes, I’m testing both in the simulator and on a device (Android). the print command shows that the code is actually working.

roaminggamer: Thaks for the tip with the code formatting, I’ve done it now. Couldn’t figure out how to do it, but now I know  :slight_smile:

I’m still stuck with this issue. The textField remains as a white block after the 8th digit has been entered.

What am I missing?

Perhaps you should try event.target.text instead of event.text

Rob

That didn’t help either. What I observe is that the defaultField probably diappears, but it leaves a white hole in the black background.

How can I restore the background?

Can you post your current code?

Rob

I think I have discovered why this “white hole” remained on the screen. telField.hasBackground = false seems to fix this. But I still have an issue…

Everything runs smoothly when tapping the screen, entering the new telephone number and then, after digit 8, the text “Lagrer…” (“Storing…”) appears and next the “ID: XXXXXXXX”. But now, if I tap again, the execution starts directly in textListener, instead of in handleIDButtonEvent.

I have tried telField.removeEventListener( “userInput”, textListener ) at the end of textListener, but with no success.

local function textListener( event ) if ( event.phase == "editing" ) then if ( string.len(event.target.text) == 8 ) then telno = event.target.text -- Write the telephone number to file local path = system.pathForFile( "georeg.txt", system.DocumentsDirectory ) local file, errorString = io.open( path, "w" ) if not file then -- Error occurred; output the cause print( "File error: " .. errorString ) else file:write( telno ) io.close( file ) end file = nil telField:removeEventListener( "userInput", textListener ) display.remove(telField) telField = nil native.setKeyboardFocus( nil ) IDText.text = "Lagrer..." timer.performWithDelay( 1000, IDShow, 1 ) end end end local function handleIDButtonEvent(event) -- Create text field system.vibrate() telField = native.newTextField( display.contentCenterX, display.contentCenterY \* 0.1, 100, 30 ) telField.hasBackground = false telField:addEventListener( "userInput", textListener ) telField.inputType = "phone" telField.align = "center" telField.font = native.newFont( native.systemFont, 16 ) telField:resizeHeightToFitFont() telField:setTextColor( 1, 1, 1 ) native.setKeyboardFocus(telField) IDText.text = "" end -- Function to handle timer event function IDShow(event) IDText.text = "ID: " .. telno end

My problem seems to be the fact that I try to “kill” the textField in another routine than I have created it. When putting the telField.removeSelf() right behind the telField = native.newTextField() it works OK. To put it simple,

local function handleIDButtonEvent(event) telField = native.newTextField( display.contentCenterX, display.contentCenterY \* 0.1, 100, 30 ) telField:removeSelf() end

 …works OK (the telField is removed immediately). However,

local function textListener( event ) telField:removeSelf() end local function handleIDButtonEvent(event) telField = native.newTextField( display.contentCenterX, display.contentCenterY \* 0.1, 100, 30 ) telField:addEventListener( "userInput", textListener ) end

…does not work. I assume something is wrong with the “local” directives, telField does not seem to be recognized outside the handleButtonEvent.

You might be running into a circumstance that we see with physics.collisions and some other things going on in call back listeners.

You’re trying to remove the object while it’s event handling function is still running. The typical solution is to wrap the remove call in a short timer:

timer.performWithDelay(10, function() telField:removeSelf(); end)

Give that a try.

Rob

Hi Rob,

That didn’t make any difference. Also the following setup fails,

local function textListener( event ) timer.performWithDelay( 1000, IDShow, 1 ) end local function handleIDButtonEvent(event) telField = native.newTextField( display.contentCenterX, display.contentCenterY \* 0.1, 100, 30 ) telField:addEventListener( "userInput", textListener ) end function IDShow(event) telField:removeSelf() end

Here the removeSelf() is absolutely outside the event handler. I have googled the topic in many ways, and it seems that I’m not the only one having issues with this.

SOLVED!

The location of the IDButton and the textField are the same. When tapping the IDButton, numerous textFields are created on top of each other. So, when executing textField.removeSelf() only the top one is removed

The simple fix is as follows,

local function textListener( event ) telField:removeSelf() end local function handleIDButtonEvent(event) if not telField then telField = native.newTextField( display.contentCenterX, display.contentCenterY \* 0.1, 100, 30 ) telField:addEventListener( "userInput", textListener ) end end

In this way only one is opened/created.

Does your print("Nummeret er " … telno) line print out?

Are you testing on a device?

  1.  Please use code formatting for future code posts (click the <> symbole when editing a post and paste your code in the popup).

  2. Prefer display.remove( obj ) to obj:removeSelf()  – The prior is safer.

  3. Be sure to clear the keyboard focus with: native.setKeyboardFocus( textField )

    display.remove( defaultField ) defaultField = nil native.setKeyboardFocus( nil )

JonPM: Yes, I’m testing both in the simulator and on a device (Android). the print command shows that the code is actually working.

roaminggamer: Thaks for the tip with the code formatting, I’ve done it now. Couldn’t figure out how to do it, but now I know  :slight_smile:

I’m still stuck with this issue. The textField remains as a white block after the 8th digit has been entered.

What am I missing?

Perhaps you should try event.target.text instead of event.text

Rob

That didn’t help either. What I observe is that the defaultField probably diappears, but it leaves a white hole in the black background.

How can I restore the background?

Can you post your current code?

Rob