views:

92

answers:

2

Hi,

I am new in iphone.I use a flag variable to play songs in avAudio player all songs are properly handeled with flag variable.we have two tabs in tab bar , i want that if any song playing then on other tab song info show.If we use that flag variable then i syncronize song info with song.But i can't access the value of flag on song info tab.I import the global file in song info file.

Please Help me any one through which i define a global integer that var i can access in all project.

+1  A: 

Put the variable in your application delegate .m file. Declare it as

extern MyType* MyVar = MyVal;

Then in your app delegate .h file

extern MyType* MyVar = MyVal;

then include that .h file wherever you need the variable.

You could also use a separate file to place globals in.

You could also place the extern in your *_prefix.h file - though I personally dont like doing that.

sylvanaar
Unwrapped global variables are evil. While this will work technically, it will make the app fragile.
TechZen
I used to think that also, now I tend to be more pragmatic. They are the right solution at a certain scale. Not every app scales infinitely.
sylvanaar
+3  A: 

Global variables are evil. As the complexity of the app grows they will cause problems that are nearly impossible to track down.

There are a few ways to handle this.

  1. Create a BOOl property in your app delegate and reference the property by referencing the app delegate. This is the most common way to implement this type of functionality.
  2. Create a custom singleton object to hold the variables. Access the variable by calling the singleton. You usually only use this one for large complex data.
  3. Save the value in the user defaults by calling +[NSUserDefaults standardUserDefaults]

In your case I think (3) would be best because you're really trying to save the application state instead of user data. Application state information belongs in the user defaults. This is particularly handy when you want to restart the app back to its previous state e.g. open the views and data that were open when it last quit.

TechZen