i have been trying to put a small form with native textfields in my app. and when the user clicks on submit button a dialog box should appear displaying a message. for that i have used native.showAlert, it worked fine until i go back to another scene in composer. its reappearing when i change my scene from the existing one. please help me resolve this issue.
here is my code:
local composer = require( “composer” )
local scene = composer.newScene()
local widget = require( “widget” )
local myApp = require( “myapp” )
local alert
local textfield
local textfield1
local textfield2
local textfield3
local textfield4
function scene:create( event )
local sceneGroup = self.view
local background = display.newImage(“images/background.png”,display.contentWidth, display.contentHeight)
–background:setFillColor( 0.95, 0.95, 0.95)
background.x = display.contentCenterX
background.y = display.contentCenterY
sceneGroup:insert(background)
local topstrip = display.newImage(“images/johnbeanstrip1.png”)
topstrip.x = display.contentCenterX
topstrip.y = display.contentCenterY - 255
sceneGroup:insert(topstrip)
textfield = native.newTextField(220,220,150,25)
textfield.text = “”
textfield:setReturnKey( “next” )
textfield1 = native.newTextField(220,270,150,25)
textfield1.text = “”
textfield1:setReturnKey( “next” )
textfield1.inputType = “number”
textfield2 = native.newTextBox(220,345,150,70)
textfield2.text = “”
textfield2.isEditable = true
textfield2.font = native.newFont( “Helvetica”, 13 )
textfield2:setReturnKey( “Done” )
textfield3 = native.newTextField(220,170,150,25)
textfield3.text = “”
textfield3:setReturnKey( “next” )
textfield4 = native.newTextField(220,120,150,25)
textfield4.text = “”
textfield4:setReturnKey( “next” )
local button4 = widget.newButton({
width = 25,
height = 25,
defaultFile = “images/dback.png”,
overFile = “images/dback.png”,
onRelease = myApp.showScreen4
})
sceneGroup:insert(button4)
button4.anchorX = 0
button4.x = 8
button4.anchorY = 0
button4.y = 12
local defaultLabel1 = display.newText( “Machine name”, 70, 120, myApp.font, 16 )
defaultLabel1:setFillColor( 0, 0, 0 )
local defaultLabel2 = display.newText( “Model”, 100, 170, myApp.font, 16 )
defaultLabel2:setFillColor( 0, 0, 0 )
local defaultLabel3 = display.newText( “Serial Number”, 70, 220, myApp.font, 16 )
defaultLabel3:setFillColor( 0, 0, 0 )
local defaultLabel4 = display.newText( “Mobile”, 100, 270, myApp.font, 16 )
defaultLabel4:setFillColor( 0, 0, 0 )
local defaultLabel5 = display.newText( “Complaint”, 87, 320, myApp.font, 16 )
defaultLabel5:setFillColor( 0, 0, 0 )
sceneGroup:insert(defaultLabel1)
sceneGroup:insert(defaultLabel2)
sceneGroup:insert(defaultLabel3)
sceneGroup:insert(defaultLabel4)
sceneGroup:insert(defaultLabel5)
local button1
button1 = widget.newButton({
--defaultFile = “images/registerbutton.png”,
–overFile = “images/registerbutton.png”,
defaultFile = “images/submitcomplaint1.png”,
overFile = “images/submitcomplaint1.png”,
width = 100
})
sceneGroup:insert(button1)
button1.x = display.contentCenterX + 10
button1.y = display.contentCenterY + 160
local function message()
alert = native.showAlert( “Corona”, “complaint registered!!.”, { “ok” }, onComplete )
end
– Show alert with two buttons
local function onComplete( event )
if ( event.action == “clicked” ) then
local i = event.index
if ( i == 1 ) then
native.cancelAlert(alert)
alert = nil
elseif ( i == 2 ) then
end
end
end
local function textListener( event )
if ( event.phase == “began” ) then
– user begins editing defaultField
print( event.text )
button1:addEventListener(“touch”, message)
elseif ( event.phase == “ended” or event.phase == “submitted” ) then
– do something with defaultField text
print( event.target.text )
elseif ( event.phase == “editing” ) then
print( event.newCharacters )
print( event.oldText )
print( event.startPosition )
print( event.text )
end
end
textfield2:addEventListener( “userInput”, textListener )
end
function scene:show( event )
local sceneGroup = self.view
end
function scene:hide( event )
local sceneGroup = self.view
–
– Clean up any native objects and Runtime listeners, timers, etc.
–
if textfield then
textfield:removeSelf()
textfield = nil
end
if textfield1 then
textfield1:removeSelf()
textfield1 = nil
end
if textfield2 then
textfield2:removeSelf()
textfield2 = nil
end
if textfield3 then
textfield3:removeSelf()
textfield3 = nil
end
if textfield4 then
textfield4:removeSelf()
textfield4 = nil
end
if alert then
alert:removeSelf()
alert = nil
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
– END OF YOUR IMPLEMENTATION
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene