2

I have images in a array but it return the NULL

I have use this code

- (void)viewDidLoad
{
    [super viewDidLoad];
    images= [[NSArray arrayWithObjects:   
                        [UIImage imageNamed:@"1.png"],
                        [UIImage imageNamed:@"2.png"],
                        [UIImage imageNamed:@"3.png"],
                        [UIImage imageNamed:@"4.png"],
                        nil] retain];  
}
-(IBAction)Next
{
    currentImage++;
    if(currentImage >= [images count])
    {
        currentImage=0;
    }

    UIImage *img=[images objectAtIndex:currentImage];
    [animalphoto setImage:img];
    NSLog(@"print:%@",currentImage);
}

first time click button images display but second time not display image and that return NULL value.. give any suggestion and source code which is apply in our code.

4

3 回答 3

0

除了打印 currentImage 之外,您的代码完全正确,因为 currentImage 是整数类型,但您%@在 NSLog 中提到了“”字符串类型。这就是为什么它显示错误。您只需将其更改为

NSLog(@"print:%d",currentImage);

我想这会对你有所帮助。

于 2012-07-06T11:49:31.860 回答
0

在您的方法中添加验证代码viewDidLoad以检查图像是否正确加载:

- (void)viewDidLoad
{
    [super viewDidLoad];
    images= [[NSArray arrayWithObjects:   
                        [UIImage imageNamed:@"1.png"],
                        [UIImage imageNamed:@"2.png"],
                        [UIImage imageNamed:@"3.png"],
                        [UIImage imageNamed:@"4.png"],
                        nil] retain];  
    NSAssert([images count] == 4, @"Failed to load one or more images");
}

你的IBAction方法应该有一个sender参数,不是吗?

-(IBAction)Next:(id)sender
{
    currentImage = (currentImage + 1) % [images count];
    NSLog(@"currentImage=%d",currentImage);
    UIImage *img=[images objectAtIndex:currentImage];
    [animalphoto setImage:img];
}

您的代码对我来说看起来不错(但我是 Mac 开发人员),所以我怀疑这是一个打包问题。

于 2012-07-06T10:56:40.547 回答
0

使用此方法和 currentImage = 0:

 -(IBAction)Next
{

  if(currentImage>=[images count])
  {
    currentImage=0;
  }

UIImage *img=[images objectAtIndex:currentImage];
[animalphoto setImage:img];
NSLog(@"print:%@",currentImage);
currentImage++;

}
于 2012-07-06T10:58:04.383 回答