Declaring an array of UIImageViews globally in Xcode

旧城冷巷雨未停 提交于 2019-12-11 16:16:00

问题


I created an array of ImageViews in a Buttonclicked method:

UIImageView *arrayOfImageViews[10]

I successfully loaded them on the screen using a loop. (Big accomplishment for this beginner!)

But I want to refer to them in other methods (e.g. touchMove).

Where do I declare arrayOfImageViews to be both an array and a class of UIImageView so that I can use them in other methods? From what I can find, I'm to declare them in the .h and above Implementation in the .m. But I can't figure out the code and location to define the object as an array of UIImageViews in anything but the original function.


回答1:


Try using a property. So in the .h file, you want to define the property something like this:


@interface MyClass: UIView {
    NSArray *arrayOfImageViews;
} 

@property (nonatomic,retain) NSArray *arrayOfImageViews;

Now in the implementation (.m file) You need to bind this property like this

@implementation MyClass 

@synthesize arrayOfImageViews;

//now you need to initialize the array of images like this (look for the method viewDidLoad or //just add it)

- (void)viewDidLoad
{
    NSMutableArray *tmpImages = [[NSMutableArray alloc]init];
    //initialize images here
    [self setArrayOfImageViews:tmpImages];
    [tmpImages release];
    [super viewDidLoad];

}

That should do it... Sorry I didn't have time to post code that actually compiles but it should help. Basically now you have a property on the class and you can access it like self.arrayOfImages, or even just arrayOfImages (from within MyClass). Also, I used NSArray which I suggest you do too. However you can also use UIImageView* arrayOfImages if you prefer.



来源:https://stackoverflow.com/questions/7958128/declaring-an-array-of-uiimageviews-globally-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!