native.newTextField not being removed

I’m trying to use native text fields in my app, but for some reason I cannot remove them from the screen. Even when I go to another scene, they remain. Any explanations?

[lua]

local scrollView

local someField

local scene = composer.newScene()

function scene:create()

    local newCharacterGroup = self.view

    local bg = display.newRect(0, 0, display.contentWidth, display.contentHeight)

    bg.anchorX = 0

    bg.anchorY = 0

    bg:setFillColor( 0, 110/255, 37/255 )

    scrollView = widget.newScrollView {

        top = 0,

        left = 0,

        height = display.contentHeight,

        width = display.contentWidth,

        hideBackground = true,

    }

    local function myButtonPress()

        someField:removeSelf()

        local function nextScene()

            composer.gotoScene(“scripts.anotherScene”, {effect = “fromTop”, time = 600})

        end

        timer.performWithDelay( 600, nextScene )

    end

    local myButton = widget.newButton {

        top = 110,

        left = display.contentWidth/2,

        width = display.contentWidth/2 - 20,

        height = 40,

        shape = “roundedRect”,

        fillColor = {default = {36/255, 107/255, 178/255}, over = {14/255, 43/255, 71/255}},

        strokeWidth = 2,

        strokeColor = {default = {.1, .1, .1}, over = {0, 0, 0}},

        cornerRadius = 3,

        onRelease = racePress,

    }

    newCharacterGroup:insert(bg)

    newCharacterGroup:insert(scrollView)

    scrollView:insert(myButton)

end

function scene:show()

    someField = native.newTextField( display.contentWidth/2, 50, display.contentWidth/2 - 20, 40 )

    someField.anchorX = 0

    someField.anchorY = 0

    scrollView:insert(someField)

    scrollView:setScrollHeight( 800 )

end

function scene:hide()

end

function scene:destroy()

end

scene:addEventListener(“create”, scene)

scene:addEventListener(“show”, scene)

scene:addEventListener(“hide”, scene)

scene:addEventListener(“destroy”, scene)

return scene

[/lua]

I’ve tried using timer.performWithDelay() to try to remove fields, I’ve tried putting the remove function in the scene:hide function. Once a native text field is on the screen, I can’t get rid of it.

I’ve figured out a solution, but I’m not sure why it works this way. When I declare the field inside the show function, I am able to remove it from within the same function, but not from any other function. Why is this?

Hi @aaron2,

This is a basic scope issue. If you create the fields in a function, and they are local to that function, they won’t be known outside of that function unless you make sure the references are known in a wider scope. If you aren’t familiar with Lua scope, it’s one of the most essential things you should learn about before proceeding. Here is a video on the topic:

http://www.youtube.com/watch?v=2ATlcGP2zMY

Best regards,

Brent

Figured it out. Was adding multiple text fields because scene:show() has two event phases, “will” and “did”. 

New code:

[lua]

if phase == “will” then

    – add field

elseif phase == “did” then

    – other stuff

end

[/lua]

Aaron2 - 

I’m having a similar problem. Tried the code you posted here (with the will/did fix) but still the field remans when I leave the scene. 

Could you post the code with the fixes in it that worked?

(I’m assuming line 42 was supposed to be myButtonPress not racePress, right?)

thank you,

leon …

Hi leon,

Yes, 42 was supposed to be myButtonPress. Making a D&D app ;) 

My fix was as follows:

  1. function scene:show(event)
  2. if event.phase == “will” then
  3.     someField = native.newTextField( display.contentWidth/2, 50, display.contentWidth/2 - 20, 40 )
  4.     someField.anchorX = 0
  5.     someField.anchorY = 0
  6.  
  7.  
  8.     scrollView:insert(someField)
  9.  
  10.     scrollView:setScrollHeight( 800 )
  11. elseif event.phase == “did” then
  12. – other stuff
  13. end
  14. end

Thanks, I’ll try that.

I’ve tried using timer.performWithDelay() to try to remove fields, I’ve tried putting the remove function in the scene:hide function. Once a native text field is on the screen, I can’t get rid of it.

I’ve figured out a solution, but I’m not sure why it works this way. When I declare the field inside the show function, I am able to remove it from within the same function, but not from any other function. Why is this?

Hi @aaron2,

This is a basic scope issue. If you create the fields in a function, and they are local to that function, they won’t be known outside of that function unless you make sure the references are known in a wider scope. If you aren’t familiar with Lua scope, it’s one of the most essential things you should learn about before proceeding. Here is a video on the topic:

http://www.youtube.com/watch?v=2ATlcGP2zMY

Best regards,

Brent

Figured it out. Was adding multiple text fields because scene:show() has two event phases, “will” and “did”. 

New code:

[lua]

if phase == “will” then

    – add field

elseif phase == “did” then

    – other stuff

end

[/lua]

Aaron2 - 

I’m having a similar problem. Tried the code you posted here (with the will/did fix) but still the field remans when I leave the scene. 

Could you post the code with the fixes in it that worked?

(I’m assuming line 42 was supposed to be myButtonPress not racePress, right?)

thank you,

leon …

Hi leon,

Yes, 42 was supposed to be myButtonPress. Making a D&D app ;) 

My fix was as follows:

  1. function scene:show(event)
  2. if event.phase == “will” then
  3.     someField = native.newTextField( display.contentWidth/2, 50, display.contentWidth/2 - 20, 40 )
  4.     someField.anchorX = 0
  5.     someField.anchorY = 0
  6.  
  7.  
  8.     scrollView:insert(someField)
  9.  
  10.     scrollView:setScrollHeight( 800 )
  11. elseif event.phase == “did” then
  12. – other stuff
  13. end
  14. end

Thanks, I’ll try that.