(Solved)Global variable not working

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Rod Edwardson
Posts: 23
Joined: Sun Mar 03, 2013 1:09 am

(Solved)Global variable not working

Post by Rod Edwardson » Mon Apr 22, 2013 8:08 pm

Hello all

I have an app that contains two cards. I am running into an issue that I can't seem to figure out. Here is some background: The first card has a multiline text control that accepts user input text. I have a global variable MyVarText that stores the text. When a user taps a button, they are taken to the second card to perform edits on the original text. The text from the variable loads fine on my iOS simulator but on actual device with 6.1.3 the variable does not load into the text box of the second card.

a) I have used MOBGUI for most controls and notice it places "development" for the environment in the controls script. (Which I have not changed to "mobile" in other apps and they have worked fine) Could this play a role?

b) You will notice by the code below that I have closed the initial card in the button script prior to opening second card. This was due to the created multiline control showing up on the second card over top of the other controls. I tried hiding it without success and closing the previous card worked so have stuck with that. May this also play a role?

If anyone has an idea as to why the MyVarText global variable is not loading on the second card on the actual device, but works on the simulator, I would sure be appreciative!


This is the first card script

Code: Select all

on preOpenCard

   # mobGUIStart ---> Inserted by mobGUI plugin
   if "MobGUILib-1365522214984.464844" is not among the lines of the stacksInUse then start using stack "MobGUILib-1365522214984.464844"
   mobGUIPreOpenCard me
   # mobGUIEnd

  
end preOpenCard

local inputID
global MyVarText
   on clearFields
   iphoneControlSet inputID, "text", empty
   
put empty into MyVarText
end clearFields

on openCard
   if environment() <> "development" then exit openCard
      iphoneControlCreate "multiline"
   put the result into inputID
   
   iphoneControlSet inputID, "rect", the rect of grc 2
   iphoneControlSet inputID, "visible", "true"
   iphoneControlSet inputID, "opaque", "false"
   iphoneControlSet inputID, "fontName", "Helvetica"
   iphoneControlSet inputID, "fontSize", 18
   iphoneControlSet inputID, "textColor", "0,0,0"
   iphoneControlSet inputID, "borderStyle", "none"
   --iphoneControlSet inputID, "clearButtonMode", "unless editing"
   iphoneControlSet inputID, "autoCapitalizationType", "sentences"
   iphoneControlSet inputID, "autoCorrectionType", "Yes"
   iphoneControlSet inputID, "autoClear", "true"
    iphoneControlSet inputID, "text", MyVarText
     end openCard

on closeCard
   iphoneControlDelete inputID
end closeCard

on inputBeginEditing
   --put iphonecontrolGet(inputID, "text") into fld 1
   --iphoneControlSet inputID, "textColor", "0,0,0"
end inputBeginEditing

on inputTextChanged
   --iphoneControlSet inputID, "textColor", "0,0,0"
   --put iphonecontrolGet(inputID, "text") into fld 1
end inputTextChanged

on inputEndEditing
   put iphonecontrolGet(inputID, "text") into fld 1
   put iphonecontrolGet(inputID, "text") into MyVarText
   iphoneControlSet inputID, "textColor", "0,0,0"
     --answer iphonecontrolGet(inputID, "text")
end inputEndEditing

  


This is the button script

Code: Select all

on touchStart pId
   mobGUITouch the long id of me
end touchStart


on mouseDown
   if the environment = "development" then touchStart 1
   close stack "Text To Analyze"
   open stack "Objective"
   focus on nothing
   end mouseDown
And this is the second card script

Code: Select all

on preOpenCard
   # mobGUIStart ---> Inserted by mobGUI plugin
   if "MobGUILib-1365522214984.464844" is not among the lines of the stacksInUse then start using stack "MobGUILib-1365522214984.464844"
   mobGUIPreOpenCard me
   # mobGUIEnd
end preOpenCard

global MyVarText

on openCard
   Put MyVarText into field "Field o" 
                      focus on nothing
                        end openCard 
                   
Last edited by Rod Edwardson on Tue Apr 23, 2013 9:49 pm, edited 1 time in total.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Global variable not working

Post by Simon » Tue Apr 23, 2013 1:55 am

Hi Rod,
Not sure why you do this:

Code: Select all

   close stack "Text To Analyze"
   open stack "Objective"
You speak of going from 1 card to another but you are going from 1 stack to another.
I'm going to make a terrible assumption that you are new to LC, I apologise if I'm wrong.

Stacks can contain multiple cards and you can move between them sharing global variables. If your 2 cards were named "Text To Analyze" and "Objective" then your button would read:
on mouseUp
go to card "Objective"
end mouseUp
along with the delete stuff.
Which brings me to another point. Not sure you need all that creating stuff, just drag an editable text field onto the card.

Ok I'm going to stop in case I'm just making a fool of myself.
Ask me and I'll tell you more.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Rod Edwardson
Posts: 23
Joined: Sun Mar 03, 2013 1:09 am

Re: (Solved)Global variable not working

Post by Rod Edwardson » Tue Apr 23, 2013 9:57 pm

Thanks for the response. Yes, I am a "complete beginner" as this forum title details. I believe you are correct on the stack/card issue. I had the global variable set on one stack and expected it to be available to pull from on another stack. What is odd is that it works on the iOS simulator, just not on the actual device. I have since gone back and started with the A B C's of livecode lessons and have a better understanding of the stack and card relationship etc. I have recreated the app with the one main stack and subsequent cards that have the other text field that I want to populate with the variable. Additionally, the multiline text box that is with MOBGUI will not work for my purposes. As it does not accept native gestures such as copy paste etc. (At least I could not get them to work) iTunes has rejected them on me in the past due to this. The creation of the control allows for those gestures. I think I'm off to the races now and thank you very much for the response!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: (Solved)Global variable not working

Post by mwieder » Tue Apr 23, 2013 10:36 pm

Rod- I'm not sure about mobGui, so I can't comment there, but you're correct about global variables. Once invoked they will stay in memory unless explicitly deleted, and you should be able to use them in different stacks within the same session.

Post Reply