在 for 循环中增加 UIImageView 变量的数字部分?
我有三个 UIImageView 变量,分别称为 myPicture1、myPicture2 和 myPicture3。在 for 循环中,我尝试将 myPicture 末尾的数字增加 1,以便我可以为三个变量中的每一个分配一个随机图像。这是下面的代码:
for (i=0; i<3; i++) {
int g = arc4random() % 19;
NSString *imagename = [NSString stringWithFormat:@"image%d.jpg",g];
UIImage *picture = [UIImage imageNamed:imagename];
UIImageView imageView = [[UIImageView alloc] initWithImage : picture];
self.myPicture1 = imageView; **This is where I want myPicture to increase to myPicture 2 and then 3 **
}
这可能吗?
谢谢大家,
马丁
I have a three UIImageView variables called myPicture1, myPicture2 and myPicture3. In a for loop I am trying to increase the number at the end of myPicture by 1 so that I can assign a random image to each of the three variables. Here is the code below :
for (i=0; i<3; i++) {
int g = arc4random() % 19;
NSString *imagename = [NSString stringWithFormat:@"image%d.jpg",g];
UIImage *picture = [UIImage imageNamed:imagename];
UIImageView imageView = [[UIImageView alloc] initWithImage : picture];
self.myPicture1 = imageView; **This is where I want myPicture to increase to myPicture 2 and then 3 **
}
Is this possible ?
Thanks all,
Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我认为你正在以一种有点尴尬的方式处理这个问题。
也许最好的方法是将您的 UIImageView 存储在 NSArray 中。
所以在你的类声明中,而不是
你会
然后你像这样创建它们:
well, I think you're approaching this in a kinda awkward way.
probably the best way to do that is to have your
UIImageView
s stored in anNSArray
.So in your class declaration, instead of
you would have
And then you create them like this: