Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 4    Views: 1023

Absentia
buzztouch Evangelist
Profile
Posts: 960
Reg: Oct 20, 2011
Alaska
20,600
10/20/12 05:19 PM (13 years ago)

Xcode - how do I use one integer in the entire application?

I'm trying to assign an integer in the appdelegate.m and then use that same integer in the menu list.m. For example, in the app delegate I would put this int badgeNum = 5; how do I make it so that badgeNum will still be equal to 5 if I put something in menu_list.m like this... theNumber = badgeNum; (i.e. "theNumber" will now be equal to 5)
 
theMonster
Code is Art
Profile
Posts: 435
Reg: Oct 18, 2011
US
8,050
like
10/20/12 09:05 PM (13 years ago)
Well, what are you trying to do exactly?
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
10/20/12 10:52 PM (13 years ago)
@theMonster: Working on your plugin tonight - finally - way behind on this but hope to have it DONE before passing out. Expect an email later :-) @Absentia: There are a few ways to manage "global" values in iOS, the easiest way is the approach you've already started. There are other ways also (like saving to user prefs, etc.) To set and access a value in your app's delegate (you can get or set it's value anywhere in your app). Do this: 1) Open your app's delegate.h file. Declare the property like all the others with: int badgeNum; Then, declare the Accessors like all the others: @property (nonatomic) int badgeNum; Done with the .h file. 2) Open the .m file for your app's delegate. Synthesize your new property like all the others at the top of the file: @synthesize badgeNum Synthesizing the property tells the compiler to automatically create the Getters and Setters for this Class Property. You won't see the getters and setters in your code but you can use them. This saves a ton of time when programming. If you're unsure what Getters and Setters are, google when you get time, it's worth understanding. Next, the badgeNum value has NO VALUE. It's nil at this point, it's usually best to give it a value, even if you're not using it so you don't accidentally try to access it before assigning a value. In the didFinishLaunchingWithOptions method (still in the .m file), give it a value, like: badgeNum = 0; (must be an integer because you declared the property as an integer. 3) Getting / setting the value anywhere in your code. In the screen you're working with (you mentioned a menu) you "get" the value or "set" the value by first getting a reference to your app's delegate file. This happens in tons of places throughout you project: //appDelegate [your app]_appDelegate *appDelegate = ([your app]_appDelegate *)[[UIApplication sharedApplication] delegate]; now you can use properties in the delegate... To get the value: int theNumber = [appDelegate badgeNumber]; This creates a new integer. If you've already declared the integer somewhere else it would be: theNumber = [appDelegate badgeNumber]; If you set the value it will be the new value anywhere else in your project until you overwrite it again: [appDelegate setBadgeNumber:5]; Of course the 5 could be any integer. The next time you do.... theNumber = [appDelegate badgeNumber]; "theNumber" will equal 5. Happy coding. Back to @theMonsters uber-cool plugin!
 
theMonster
Code is Art
Profile
Posts: 435
Reg: Oct 18, 2011
US
8,050
like
10/20/12 10:55 PM (13 years ago)
@David Thanks man! So excited! Also, if your literally trying to set the apps icon badge count, you set it like this: [UIApplication sharedApplication].applicationIconBadgeNumber = 1;
 
Absentia
buzztouch Evangelist
Profile
Posts: 960
Reg: Oct 20, 2011
Alaska
20,600
like
10/21/12 02:11 AM (13 years ago)
@David - thank you! I have been trying to get this badge thing figured out for the last three days, and your post helped me to accomplish exactly what I wanted. Can't thank you enough man, this has been such a huge headache @theMonster - I wish it was that easy - I've been trying to get the badge to show on an Appoxee inbox. This means running a method to check for an updated "new message" number (which, as far as I can tell, can only be run in the delegate). What made it hard for me was that I wanted to show the Appoxxe button on a Socialize action bar which I don't think can be implemented in the delegate. Hence, the instruction on how to get the new message number from the delegate.m to the menulist.m was the final piece of the puzzle PS are you guys referring to the iAP plugin? - man I hope so. That's another thing I've had a tough time implementing
 

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.