Global variable problem
I am new to iphone. I am using a flag variable to play songs in avAudio player. All songs are properly handled with the flag variable.we have two tabs in the tab bar, I want if any song play then other song information show.If we use this flag variable then I sync the song information with the song ... But I cannot access the flag value in the song info tab. I am importing a global file into a song information file.
Please help me anyone through which I define a global integer which var I can get in all projects.
a source to share
Global variables are evil. As the complexity of the application grows, they will cause problems that are almost impossible to trace.
There are several ways to handle this.
- Create a BOOl property in the app's delegate and specify the property by specifying the app delegate. This is the most common way to implement this type of functionality.
- Create a custom singleton object to store variables. Access the variable by calling singleton. Usually you only use this for big complex data.
- Store the value in the user defaults by calling
+[NSUserDefaults standardUserDefaults]
In your case, I think (3) would be better, because you are really trying to save application state instead of user data. Application state information belongs to user defaults. This is especially useful if you want to restart the application back to its previous state, for example. open the views and data that were open when it last closed.
a source to share
Place a variable in your file to delegate .m file. Declare it as
extern MyType* MyVar = MyVal;
Then in your delegate delegate .h file
extern MyType* MyVar = MyVal;
then include that .h file where you need the variable.
You can also use a separate file to host the global tables.
You can also place extern in the * _prefix.h file, although I personally don't like that.
a source to share