Need a second opinion re native.newTextField

This code used to work.

local usernameField, username  
  
local function onUsername( event )  
 if ( "began" == event.phase ) then  
 caption.text=""  
 usernameField.text = ""  
 elseif ( "submitted" == event.phase ) then  
 usernameField:setTextColor( 51, 51, 122, 255 )  
 username=usernameField.text  
 native.setKeyboardFocus( nil )  
 end  
end  
  
usernameField = native.newTextField( 50, 65, 220, 36, onUsername )  
usernameField.font = native.newFont( native.systemFontBold, 24 )  
usernameField.text = "Enter User Name"  
usernameField:setTextColor( 51, 51, 122, 45 )  

When the textfield got focus, keyboard would appear and usernameField.text would blank out.

I’m trying to incorporate this into my login page that uses Storyboard and latest Corona build and now the field won’t blank.

I’ve been checking the new listener API as I’m getting the depreciated warning:
WARNING: The ‘listener’ argument to native.newTextField( left, top, width, height [, listener] ) is deprecated. Call the object method o:addEventListener( ‘userInput’, listener ) instead.

Can’t quite wrap my head around it. Any help much appreciated.

[import]uid: 6547 topic_id: 24488 reply_id: 324488[/import]

Jump over to this Post , it will fix you right up.
–Croisened
[import]uid: 48203 topic_id: 24488 reply_id: 99104[/import]

Thank you Croisened.

I made the change but may be in dense mode today. I don’t see any change in results.
I don’t thing the “began” never gets hit as usernameField.text never goes blank.

I assume it’s correct that in simulator the Event would not fire? I thought it used to in the old scheme but could be wrong.

This is my whole scene. Can you spot where I’m screwing up? This scene is called my main. main only does a bit of setup and the calls this page.

---------------------------------------------------------------------------------  
--  
-- scene\_login.lua  
--  
---------------------------------------------------------------------------------  
  
local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
  
---------------------------------------------------------------------------------  
-- BEGINNING OF YOUR IMPLEMENTATION  
---------------------------------------------------------------------------------  
local usernameField, username  
  
-- Called when the scene's view does not exist:  
function scene:createScene( event )  
 local screenGroup = self.view  
  
 image = display.newImage( "bg2.jpg" )  
 screenGroup:insert( image )  
  
 imageLogin = display.newImage( "images/dialog\_bkg.png" )  
 screenGroup:insert( imageLogin )  
  
 myImage = display.newImage( "images/bdlogoStationary.png",107,270)  
 screenGroup:insert(myImage)  
  
 text1 = display.newText( "LOGIN", 0, 0, native.systemFontBold, 24 )  
 text1:setTextColor( 255 )  
 text1:setReferencePoint( display.CenterReferencePoint )  
 text1.x, text1.y = display.contentWidth \* 0.5, 40  
 screenGroup:insert( text1 )  
  
--[[  
Hopefully this is my screwup. Function duplicated further down. Back to testing but   
thought I better edit this post first.  
  
local function onUsername( event )  
 print("in onusername")  
 if ( "began" == event.phase ) then  
 caption.text=""  
 usernameField.text = ""  
 elseif ( "submitted" == event.phase ) then  
 usernameField:setTextColor( 51, 51, 122, 255 )  
 username=usernameField.text  
 native.setKeyboardFocus( nil )  
 end  
end  
--]]  
  
local function onLogin( event )  
 native.setKeyboardFocus( nil )  
  
 if \_G.isSimulator then  
 username = "Byted"  
 password="sim-pass"  
 ip = "192.168.2.110"  
 port = "5014"  
 else  
 username = usernameField.text  
 end  
 \_G.username=username  
 print("username="..username.." / \_G: "..\_G.username)  
 if username ~= "" then  
 print("Ready for scene change")  
 storyboard.gotoScene( "scene1", "slideUp", 800 )  
 else  
 caption.text="Invalid Username or Password"  
 end  
end  
  
print("\nviewrow:", \_G\_viewrow)  
  
local function onUsername( event )  
 print("in onusername")  
 if ( "began" == event.phase ) then  
 print("onUsername - began")  
 usernameField.text = ""  
 elseif ( "ended" == event.phase ) then  
 print("onUsername - ended")  
 elseif ( "submitted" == event.phase ) then  
 usernameField:setTextColor( 51, 51, 122, 255 )  
 native.setKeyboardFocus( nil )  
 end  
end  
  
 usernameField = native.newTextField( 50, 65, 220, 36)  
 usernameField:addEventListener("UserInput", onUsername)  
 usernameField.font = native.newFont( native.systemFontBold, 24 )  
 usernameField.text = "Enter User Name"  
 usernameField:setTextColor( 51, 51, 122, 45 )  
  
 local loginButton = ui.newButton{  
 default = "images/smallButton.png",  
 over = "images/smallButtonOver.png",  
 onPress = onLogin,  
 text = "LOG IN",  
 font = "Helvetica-Bold",  
 size = 13,  
 textColor = { 225, 225, 225, 255 },  
 emboss = true,  
}  
screenGroup:insert( loginButton )  
loginButton.x = 220  
loginButton.y = 165  
  
 print( "\nlogin: createScene event" )  
end  
  
-- Called immediately after scene has moved onscreen:  
function scene:enterScene( event )  
  
 print( "login: enterScene event" )  
  
local function main()  
  
 reply = start()  
  
 -----------------------------------  
 -- Return  
 -----------------------------------  
 return reply  
end  
  
logedin = main()  
print("After logedin:",logedin)   
end  
-- Called when scene is about to move offscreen:  
function scene:exitScene()  
  
 print( "login: exitScene event\n" )  
  
end  
  
-- Called prior to the removal of scene's "view" (display group)  
function scene:destroyScene( event )  
  
 print( "((destroying scene login's view))" )  
end  
  
---------------------------------------------------------------------------------  
-- END OF YOUR IMPLEMENTATION  
---------------------------------------------------------------------------------  
  
-- "createScene" event is dispatched if scene's view does not exist  
scene:addEventListener( "createScene", scene )  
  
-- "enterScene" event is dispatched whenever scene transition has finished  
scene:addEventListener( "enterScene", scene )  
  
-- "exitScene" event is dispatched before next scene's transition begins  
scene:addEventListener( "exitScene", scene )  
  
-- "destroyScene" event is dispatched before view is unloaded, which can be  
-- automatically unloaded in low memory situations, or explicitly via a call to  
-- storyboard.purgeScene() or storyboard.removeScene().  
scene:addEventListener( "destroyScene", scene )  
  
---------------------------------------------------------------------------------  
  
return scene  

Apologies for the long post but I’m desperate :slight_smile:

Edit: YIKES!
onusername function appears twice. The pitfalls of careless cut’n paste. [import]uid: 6547 topic_id: 24488 reply_id: 99119[/import]