I want to use single imageview in all classes so i want to declare it as global. Can anyone please tell me how to declare it as global.
-
Is this to be used across multiple view controllers? – Mick MacCallum Mar 08 '12 at 05:50
-
IMHO, you can attach the image view to [a singleton instance][1]. [1]: http://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c – ZhangChn Mar 08 '12 at 05:56
4 Answers
you can create it in appdelegate and use it
AppDelegate *localVar = [[UIApplication sharedApplication] delegate]; // OK
localVar.imageView=[...];
- 12,237
- 11
- 61
- 115
Yes, there is, you may use Singleton Class.
here the tutorial http://www.galloway.me.uk/tutorials/singleton-classes/
- 34,562
- 12
- 96
- 109
- 12,195
- 5
- 40
- 66
Not sure if this is something that really vibes with how iOS does things, but if you really want to, you can declare the variable as a forward declaration in some .h file:
extern something_t *x;
and then in a .c or .m file, actually define the variable:
something_t *x;
If you want to initialize it, you can place an initialization wherever you deem appropriate:
- (void) applicationDidFinishLaunching:(NSNotification*) notice {
x = [[Something_T alloc] init];
}
Again, not sure this is the right way to solve whatever problem you might be having. A singleton might be a more Cocoa-esque idea.
- 24,966
- 29
- 102
- 154
Declare the variable in Yourapp_Prefix.pch file in the project. The file is located in 'other sources' group.
UIImageView *imgv;
Now you can access the object imgv in any class.
- 2,647
- 20
- 26