views:

102

answers:

3

i want to access the same variable in all view controllers ....

+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
but i am not able to access this variable no longer in other views
lak in iphone
are you #import'ing the header that declares the variable in each of your view files?
Seamus Campbell
+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
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