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
  • 203
  • 1
  • 2
  • 6

4 Answers4

1

you can create it in appdelegate and use it

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

Yes, there is, you may use Singleton Class.

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

mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
HelmiB
  • 12,303
  • 5
  • 41
  • 68
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
  • 26,898
  • 33
  • 107
  • 159
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,685
  • 21
  • 27