Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

textField save help
Started by alberto.diego.chisilevich Dec 23 2018 04:09 AM

3 replies to this topic

Best Answer roaminggamer , 23 December 2018 - 11:26 AM

Download: 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/12/fieldSaver.zip

 

 

Using SSK2 features I'd do this:

io.output():setvbuf("no")
display.setStatusBar(display.HiddenStatusBar)
-- =====================================================
require "ssk2.loadSSK"
_G.ssk.init( { measure = false } )
-- =====================================================
--
-- This is 'helper' code made to simplify and homogenize making multiple fields
-- that auto-save changes and auto-restore them on next-load.
--
local persist = ssk.persist

local fields = {}

local function userInput( self, event )
   if ( event.phase == "began" ) then
   elseif ( event.phase == "ended" or event.phase == "submitted" ) then
      persist.set( "fieldValues.json", self._id, self.text )
   elseif ( event.phase == "editing" ) then
      persist.set( "fieldValues.json", self._id, self.text )
   end
   return false
end

local function makeField( x, y, w, h, id, params )
   params = params or {}
   --
   local field = native.newTextField( x, y, w, h )
   field._id = "field_" .. id
   field.userInput = userInput
   field:addEventListener( "userInput" )
   for k,v in pairs(params) do
      field[k] = v
   end
   
   -- Restore prior value for this field if it exists
   --
   local restoreValue = persist.get( "fieldValues.json", field._id ) or ""
   field.text = restoreValue
   --
   return field
end

--
-- Now use the helper from above to make some fields
--
local label = display.newText( "User Name:", centerX - 160, centerY - 30, nil, 20)
label.anchorX = 1
local userName = makeField( centerX, label.y, 300, 40, 'userName' )

local label = display.newText( "Password:", centerX - 160, centerY + 30, nil, 20)
label.anchorX = 1
local password = makeField( centerX, label.y, 300, 40, 'password', { isSecure = true } )

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

alberto.diego.chisilevich

[GLOBAL: userInfoPane.html]
alberto.diego.chisilevich
  • Observer

  • 28 posts
  • Corona SDK

hi

i am trying for sometime now to use the textField user input and save it into a table and i can't understand it.

itried the docs and also some youtube videos but i am to stupid to get it right.

please help.

 

what i am actually trying to do is :

text field input user to create a table ,then another text field for more input user inside the first table.

 

example : project name > list of things to do > do that

                                                                        do this 

                                                                        do it

 

i can 't really show you i have down until now because it's unusable .

 

 

i am really frustrated ,i am trying to learn for about more than three months by now but i can 't do things,

i can read code but when i try to do my own it's just a mess and full of errors.



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,492 posts
  • Corona SDK

  Best Answer

Download: 

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/12/fieldSaver.zip

 

 

Using SSK2 features I'd do this:

io.output():setvbuf("no")
display.setStatusBar(display.HiddenStatusBar)
-- =====================================================
require "ssk2.loadSSK"
_G.ssk.init( { measure = false } )
-- =====================================================
--
-- This is 'helper' code made to simplify and homogenize making multiple fields
-- that auto-save changes and auto-restore them on next-load.
--
local persist = ssk.persist

local fields = {}

local function userInput( self, event )
   if ( event.phase == "began" ) then
   elseif ( event.phase == "ended" or event.phase == "submitted" ) then
      persist.set( "fieldValues.json", self._id, self.text )
   elseif ( event.phase == "editing" ) then
      persist.set( "fieldValues.json", self._id, self.text )
   end
   return false
end

local function makeField( x, y, w, h, id, params )
   params = params or {}
   --
   local field = native.newTextField( x, y, w, h )
   field._id = "field_" .. id
   field.userInput = userInput
   field:addEventListener( "userInput" )
   for k,v in pairs(params) do
      field[k] = v
   end
   
   -- Restore prior value for this field if it exists
   --
   local restoreValue = persist.get( "fieldValues.json", field._id ) or ""
   field.text = restoreValue
   --
   return field
end

--
-- Now use the helper from above to make some fields
--
local label = display.newText( "User Name:", centerX - 160, centerY - 30, nil, 20)
label.anchorX = 1
local userName = makeField( centerX, label.y, 300, 40, 'userName' )

local label = display.newText( "Password:", centerX - 160, centerY + 30, nil, 20)
label.anchorX = 1
local password = makeField( centerX, label.y, 300, 40, 'password', { isSecure = true } )


  • alberto.diego.chisilevich likes this

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,492 posts
  • Corona SDK

If you don't like my example above or if it does not suit your usage, you can always use tables to hold the values of fields, then save and restore those tables on your own.

 

SSK2 comes with extensions for table save/restore too:

 

https://roaminggamer.github.io/RGDocs/pages/SSK2/extensions/#saving-loading-tables

 

I am not providing an example of that usage though.



[TOPIC: post.html]
#4

alberto.diego.chisilevich

[GLOBAL: userInfoPane.html]
alberto.diego.chisilevich
  • Observer

  • 28 posts
  • Corona SDK

wow! thanks! it's almost perfect,i just need to change some things to fit my needs.

you are the greatest.

i hope one day i will be able to code like you.




[topic_controls]
[/topic_controls]