Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 10    Views: 76

NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
04/13/14 11:28 AM (10 years ago)

Global Variable question (using 1H4B)

I know I need to find a good link or book and go back to learn the basics for variables, but for this weekend trying to learn as I go… I was hoping someone can point out what I am doing wrong. - Thanks! ------------------------ I had tried setting one approach (a) setting up in appDelegate for what I thought would be global variable and then tried (b) in the Plugin so it could be referenced by various screens of the same Plugin. The goal is to have variable that will be set on one screen, and then referenced and used to save a choice out via setPref on another screen of the sme Plugin. After that getPref will be used. Here is what I tried using the 1H4B Plugin and using buttons to set value == myapp_appDelegate.h ========== //added my variable @property (nonatomic, retain) NSString *myTempValue; == myapp_appDelegate.m ========== @synthesize myTempValue; //Initialized Temp Value NSString *myTempValue = @"0"; Then in a Plugin (1H4B) == somePlugin.h ========== @synthesize myTempValue; //Needed? == somePlugin.m ========== == screen 1 of plugin for selection ==================== -(IBAction)cr_button1Tap:(id)sender;{ //confirmed that this is setting the value myTempValue = @"0"; -(IBAction)cr_button2Tap:(id)sender; // confirmed that this is setting the value myTempValue = @"1"; == screen2 of plugin used for preview and save ========= // Using for "OK Button" after preview -(IBAction)cr_button8Tap:(id)sender;{ // at this point the value of myTempValue is nil :( // if I hard code here the value will be saved. NSString *myTempValue = @"0"; //hard-coded for test [BT_strings setPrefString:@"myThemeColor" valueOfPref: myTempValue];
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
04/13/14 12:06 PM (10 years ago)
A couple of things: 1) You correctly set the variable in the appDelegate, but you never reference back to it. Instead you create a new variable (with the same name) in your plugin files. That won't work. To reference the appDelegate variable, use this in any plugin file: BT_appDelegate *appDelegate = (BT_appDelegate*)[[UIApplication sharedApplication] delegate]; NSLog(@"current value of myTempValue is:%@", appDelegate.myTempValue); NSLog(@"setting new value to 1..."); appDelegate.myTempValue = @"1"; NSLog(@"new value of myTempValue is:%@", appDelegate.myTempValue); 2) Why use the appDelegate at all? As you said, you're ultimately saving this to a user preference anyway. Just use that approach: plugin 1: setting the value [BT_strings setPrefString:@"myTempValue" valueOfPref: @"1"]; plugin 2: getting the value NSString *myTempValue = [BT_strings getPrefString:@"myTempValue"]; NSLog(@"value of myTempValue is:%@", myTempValue);
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
04/13/14 12:07 PM (10 years ago)
disclaimer: I wrote that code by hand in the form window here, so it's possible I had a misspelling somewhere. if so, sorry!
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
04/13/14 12:56 PM (10 years ago)
Thanks Chris!! Especially for the tip to use NSLog. Much better than me setting break points and examining value. Always seems so obvious AFTER seeing the code ;) Also typing on the road. The reason for not just using setpref after they choose is because based upon their choice,I display what the graphic result will look like on the next screen. On that second screen they either Cancel or OK to save. Hopefully will look and act like the iOS settings for wallpaper on an iPhone.
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
04/13/14 01:00 PM (10 years ago)
Sure wish I could have made it to Las Vegas for code camp...
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
04/13/14 07:14 PM (10 years ago)
i see. Well, if you want a quick and dirty way to do it, use two preferences. One for the temporary value and one for the permanent value. Have the first screen clear the value of the temporary one on the viewWillAppear method so it resets each time, and use the second one for the persistent data. Or, if it's just these two plugins talking to each other in this way, pass the data along to the second one when that screen is loaded. To do that, just add to the JSON being passed by altering the screenData.jsonVars property.
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
04/13/14 07:14 PM (10 years ago)
always multiple ways to approach a problem :)
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
04/13/14 09:55 PM (10 years ago)
The alteration of screenData would only be done in RAM. The alteration will not be saved into the cached version of the bt_config file. Even if it did get saved into the cache, it would then be blown away by the next refresh of the config. In most cases, It is best to use the NSUserDefaults. -- Niraj
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
04/13/14 10:28 PM (10 years ago)
Well, modifying the screenData wouldn't be persistent, true. He would still need to save the setting to a preference file in the secondary screen. It just prevents the need to mess with the appDelegate.
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
04/14/14 04:18 AM (10 years ago)
Went with the appDelegate approach and works great. No right/wrong- but after not being able to get it right before- for now that is where it found it's home ;)
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
04/14/14 06:59 PM (10 years ago)
Like several other here I have changed the NavBarTitleTextColor in the appDelegate.m (in my case the color depends on a getPref value) Once my iOS7 app is running, and I use the new screens above to change all of the other color settings, I am left with one item remaining to change the color... the original NavBarTitleTextColor which was set when the App was launched. Is there a way to change the NavBarTitleTextColor AFTER the App is running (ie not in the appDelegate?)
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.