I have a text field on one page that a person can enter a number in. I would like the data entered in that text field to be used on the next page. So the person does not have to enter that information twice, how would I structure that coding to transfer to the textfield on the next page? I am using director class and the text fields are native.textfields using the native numbers keyboard.
I am calling the text field on the first page/screen numberFieldOne and the text field on the second page/screen numberFieldTwo.
Michelle [import]uid: 72372 topic_id: 13666 reply_id: 313666[/import]
assign the value of textfield to a global variable. (use textfield.text)
you can create the variable as a global variable in main.lua and in other screens you can set and assign the value to and from _G.variable. [import]uid: 71210 topic_id: 13666 reply_id: 50207[/import]
Okay I don’t know how to do that but will read up on it. I do know that I remember seeing that global variables slow down performance and that you should always use local. This document was telling me that. I will do it if it’s the only way though.
http://developer.anscamobile.com/content/performance-and-optimization
[import]uid: 72372 topic_id: 13666 reply_id: 50224[/import]
If I understand correctly (and please correct me if I’m wrong) the performance problem with globals is looking them up and if you use lots of them it will hurt performance.
_G isn’t all the global variables, but a single global variable that contains a table so you’re only fetching one (abeit big) variable.
[import]uid: 19626 topic_id: 13666 reply_id: 50237[/import]
Robmiracle
You have the right understanding so how do I set that up? I just want to take one number from one screen and show it on the next screen.
The location for the number I want to move is numberFieldOne and I just want to move it to the next screen in numberFieldTwo or numberFieldThree.
[import]uid: 72372 topic_id: 13666 reply_id: 50280[/import]
main.lua
[lua]local function numberOneCallBack(event)
if ( “submitted” == event.phase ) or ( “ended” == event.phase) then
_G.numberOne = numberOneField.text
native.setKeyboardFocus( nil )
end
end
numberOneField = native.newTextField( 52, 170, 200, 28, numberOneCallback)[/lua]
screen2.lua
[lua]local function numberTwoCallBack(event)
if ( “submitted” == event.phase ) or ( “ended” == event.phase) then
_G.numberTwo = numberTwoField.text
native.setKeyboardFocus( nil )
end
end
numberTwoField = native.newTextField( 52, 170, 200, 28, numberTwoCallback)
numberTwoField.text = _G.numberOne[/lua]
Something like that should work! [import]uid: 19626 topic_id: 13666 reply_id: 50292[/import]
Okay I tried it but when I did the build for my app the box is showing up on every screen. What do I need to input to make that stop? Do I have to put it in a local group in the main? or in the screen2.lua you call out? Also with the coding for the screen2.lua, where in that file do I need to put that coding? Does it have to be after a specific section? [import]uid: 72372 topic_id: 13666 reply_id: 50306[/import]
the objects returned by native.newKeyboard() are like any display object. You have to remove them when you change screens or add them to a group and remove the group when you are done with the screen.
Its hard to advise you where to put it not knowing your code structure. If you are using Director to switch screens, then you could put it all in your “new()” function and make sure to add the object returned into localGroup, i.e. localGroup:insert(numberOneTextField)
[import]uid: 19626 topic_id: 13666 reply_id: 50308[/import]
Okay I added the information and it’s still not working properly. this is my main
[lua]display.setStatusBar (display.HiddenStatusBar)
–> Hides the status bar
local director = require (“director”)
–> Imports director
local mainGroup = display.newGroup()
–> Creates a main group
local function main()
–> Adds main function
mainGroup:insert(director.directorView)
–> Adds the group from director
director:changeScene(“loadtitlescreen”)
–> Change the scene, no effects
local function numberFieldOneCallBack(event)
if ( “submitted” == event.phase ) or ( “ended” == event.phase) then
_G.numberFieldOne = numberFieldOne.text
native.setKeyboardFocus( nil )
end
end
numberFieldOne = native.newTextField( 52, 170, 200, 28, numberFieldOneCallback)
return true
end
main()[/lua]
this is the section of my next screen. I tried to put in the same format as my other textblocks and I don’t have a removal with them. I just added them to a local group and that worked. It is not working with this global variable though.
[lua]local function onnumberFieldTwo( event )
if ( “ended” == event.phase ) or ( “submitted” == event.phase ) then
native.setKeyboardFocus( numberFieldThree )
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text
end
end
local function onnumberFieldThree( event )
if ( “ended” == event.phase ) or ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text
end
end
local function onnumberFieldFour( event )
if ( “ended” == event.phase ) or ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text
end
end
function textFunction()
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text
end
–> callback function begins
local function numberFieldTwoCallBack(event)
if ( “submitted” == event.phase ) or ( “ended” == event.phase) then
_G.numberFieldTwo = numberFieldTwo.text
native.setKeyboardFocus( nil )
end
end
numberFieldTwo = native.newTextField( 180, 125, 120, tHeight, numberFieldTwoCallback)
numberFieldTwo.text = _G.numberFieldOne
–>callback function ends
numberFieldTwo = native.newTextField( 180, 125, 120, tHeight, onnumberFieldTwo )
numberFieldTwo.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldTwo.inputType = “number”
numberFieldThree = native.newTextField( 180, 180, 120, tHeight, onnumberFieldThree )
numberFieldThree.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldThree.inputType = “number”
numberFieldFour = native.newTextField( 180, 245, 120, tHeight, onnumberFieldFour )
numberFieldFour.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldFour.inputType = “number”
– Add fields to our new group
localGroup:insert(numberFieldOne)
localGroup:insert(numberFieldTwo)
localGroup:insert(numberFieldThree)
localGroup:insert(numberFieldFour)
numberFieldTwo:addEventListener(“onChange”, textFunction)
numberFieldThree:addEventListener(“onChange”, textFunction)
–>MUST return a display.newGroup()
–> This is how we end every file except for director and main, as mentioned in my first comment
return localGroup
end[/lua]
[import]uid: 72372 topic_id: 13666 reply_id: 50314[/import]
When I said main.lua earlier, that was being generic. If you’re using director, which now that you’ve posted your code, you are, you would not put it in main.lua. All main.lua does is load director and execute in your case your “loadtitlesscreen.lua” file.
You would put your text field in whatever Lua file you want it to appear in.
The way you described it above. you would have one screen with one text field. It would collect one number, then switch to the 2nd screen, and pre-populate a field with the number from the previous screen.
After seeing your code above, I’m not sure that’s what you are really trying to do. [import]uid: 19626 topic_id: 13666 reply_id: 50316[/import]
No that is exactly what I’m trying to do. The way the code is right now the person can enter in both of those fields but one of the numbers they will be entering is from the previous screen. So I thought why not just move the number from that screen to this screen.
The reason I had not thought of it before is due to the fact that the number that will come from the previous screen would have to go in a specific block depending on it’s size. Meaning if the number from the previous screen is larger than the new number they choose on the next screen it goes in the first block but if its smaller it goes in the 2nd block. I thought that would be too much coding and was just trying to keep it simple.
So with the info you gave earlier the main.lua code would go on the screen that I am pulling the number from and the other code would go on the screen where the number would go?
[import]uid: 72372 topic_id: 13666 reply_id: 50323[/import]
I can see that you have created 2 callback or listener frunction for numberFieldTwo, (onnumberFieldTwo and numberFieldTwoCallBack)
the one linked to the textfield is onnumberFieldTwo . so that leaves everything given in numberFieldTwoCallBack uncalled.
try putting
[lua]_G.numberFieldTwo = numberFieldTwo.text[/lua]
inside onnumberFieldTwo event.
[import]uid: 71210 topic_id: 13666 reply_id: 50325[/import]
try this code
main.lua
[lua]display.setStatusBar (display.HiddenStatusBar)
–> Hides the status bar
local director = require (“director”)
–> Imports director
local mainGroup = display.newGroup()
–> Creates a main group
numberOne = 0
local function main()
–> Adds main function
local function numberFieldOneCallBack(event)
if ( “submitted” == event.phase ) or ( “ended” == event.phase) then
_G.numberOne = numberFieldOne.text
native.setKeyboardFocus( nil )
mainGroup:insert(director.directorView)
–> Adds the group from director
director:changeScene(“scene2”)
–> Change the scene, no effects
end
end
numberFieldOne = native.newTextField( 52, 170, 200, 28, numberFieldOneCallback)
return true
end
main()[/lua]
scene2.lua
[lua]module(…, package.seeall)
function new()
local function onnumberFieldTwo( event )
if ( “ended” == event.phase ) or ( “submitted” == event.phase ) then
native.setKeyboardFocus( numberFieldThree )
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text
_G.numberTwo = numberFieldTwo.text
end
end
local function onnumberFieldThree( event )
if ( “ended” == event.phase ) or ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text
end
end
local function onnumberFieldFour( event )
if ( “ended” == event.phase ) or ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text
end
end
function textFunction()
numberFieldFour.text = numberFieldTwo.text - numberFieldThree.text
end
numberFieldTwo = native.newTextField( 180, 125, 120, tHeight, numberFieldTwoCallback)
numberFieldTwo.text = _G.numberOne
–>callback function ends
numberFieldTwo = native.newTextField( 180, 125, 120, tHeight, onnumberFieldTwo )
numberFieldTwo.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldTwo.inputType = “number”
numberFieldThree = native.newTextField( 180, 180, 120, tHeight, onnumberFieldThree )
numberFieldThree.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldThree.inputType = “number”
numberFieldFour = native.newTextField( 180, 245, 120, tHeight, onnumberFieldFour )
numberFieldFour.font = native.newFont( native.systemFontBold, inputFontSize )
numberFieldFour.inputType = “number”
– Add fields to our new group
localGroup:insert(numberFieldOne)
localGroup:insert(numberFieldTwo)
localGroup:insert(numberFieldThree)
localGroup:insert(numberFieldFour)
numberFieldTwo:addEventListener(“onChange”, textFunction)
numberFieldThree:addEventListener(“onChange”, textFunction)
–>MUST return a display.newGroup()
return localGroup
end[/lua]
[import]uid: 71210 topic_id: 13666 reply_id: 50330[/import]