2

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.

surendher raja
  • 193
  • 1
  • 1
  • 5

4 Answers4

1

you can create it in appdelegate and use it

 AppDelegate *localVar = [[UIApplication sharedApplication] delegate]; // OK
    localVar.imageView=[...];
Ankit Srivastava
  • 12,237
  • 11
  • 61
  • 115
1

Yes, there is, you may use Singleton Class.

here the tutorial http://www.galloway.me.uk/tutorials/singleton-classes/

mattjgalloway
  • 34,562
  • 12
  • 96
  • 109
HelmiB
  • 12,195
  • 5
  • 40
  • 66
0

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.

alexgolec
  • 24,966
  • 29
  • 102
  • 154
0

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.

ArunGJ
  • 2,647
  • 20
  • 26