i want to access the same variable in all view controllers ....
views:
102answers:
3
Q:
how to declare a variable as extern in objectivec,so that i can access that in any view controller
+1
A:
extern is a C keyword, and works the same in Objective-C as in straight C. In your header file, declare your variable:
extern NSString *myGlobal;
And then set it in your .m file.
However, this is frequently a poor coding practice; it is generally preferable to explicitly hand your view controllers some kind of state object or data source.
Seamus Campbell
2010-08-04 20:15:47
but i am not able to access this variable no longer in other views
lak in iphone
2010-08-04 21:01:04
are you #import'ing the header that declares the variable in each of your view files?
Seamus Campbell
2010-08-04 21:10:56
+1
A:
Instead of putting in an extern for variables, store your data in the AppDelegate instance - or in some other singleton. Then you can get to it, and modify it, from anywhere.
Kendall Helmstetter Gelner
2010-08-04 21:17:19
A:
While I'll admit I still use the AppDelegate solution quite often, I believe the singleton design pattern is a better solution. Here's a link to a solution and the reasoning.
ACBurk
2010-08-04 21:23:40