如何在 Xcode 中实现图像数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12183425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I implement an image array in Xcode?
提问by user1633930
I'm trying to create an array of images for an imageView and then change those images when a button is pressed. I'm just testing the ability for the button to cause a method to get and display the image from the array. What am I doing wrong?
我正在尝试为 imageView 创建一组图像,然后在按下按钮时更改这些图像。我只是测试按钮的能力,使方法从数组中获取和显示图像。我究竟做错了什么?
This is the code I have so far:
这是我到目前为止的代码:
- (void)viewDidLoad {
NSMutableArray *imagesArray = [[NSMutableArray alloc] initWithObjects:@"Image1.JPG", @"Image2.JPG", nil];
}
-(IBAction)img1 {
[imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:0]]];
}
I also get the warning 'unused variable 'imagesArray'
我也收到警告'未使用的变量'imagesArray'
Thanks for helping!
感谢您的帮助!
采纳答案by Andrei G.
you are not retaining NSMutableArray *imagesArray
, it goes out of scope as soon as viewDidLoad completes.
您没有保留NSMutableArray *imagesArray
,一旦 viewDidLoad 完成,它就会超出范围。
try this:
尝试这个:
declare instance variable NSMutableArray *imagesArray;
声明实例变量 NSMutableArray *imagesArray;
and init using
并初始化使用
imagesArray = [[NSMutableArray alloc] initWithObjects:@"Image1.JPG", @"Image2.JPG", nil];
回答by Adam
I guess you declared your array twice. Change your load method to this and try again.
我猜你两次声明了你的数组。将您的加载方法更改为此并重试。
- (void)viewDidLoad {
imagesArray = [[NSMutableArray alloc] initWithObjects:@"Image1.JPG", @"Image2.JPG", nil];
}