在Xcode中全局声明一个UIImageViews数组 [英] Declaring an array of UIImageViews globally in Xcode
问题描述
我通过Buttonclicked方法创建了ImageViews数组:
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!)
但是我想用其他方法来引用它们(例如 touchMove
)
But I want to refer to them in other methods (e.g. touchMove
).
我在哪里声明 arrayOfImageViews
既是数组又是 UIImageView的类
以便我可以在其他方法中使用它们?据我所知,我要在 .h
和更高版本中的 .m
中声明它们。但是我无法弄清楚将对象定义为UIImageViews数组的代码和位置,除了原始函数之外。
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.
推荐答案
尝试使用属性。因此,在 .h
文件中,您想要定义以下属性:
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;
现在正在执行中( .m
文件),您需要像这样绑定此属性
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];
}
应该这样做...对不起,我没有有时间发布实际可编译的代码,但这应该有所帮助。基本上,现在您在该类上有了一个属性,可以像 self.arrayOfImages
或什至 arrayOfImages
(来自 MyClass
)。另外,我建议使用 NSArray
。但是,如果愿意,还可以使用 UIImageView * arrayOfImages
。
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.
这篇关于在Xcode中全局声明一个UIImageViews数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!