单击下一步按钮时如何逐个显示图像?

时间:2012-07-06 10:46:24

标签: ios objective-c

我在数组中有图像,但它返回NULL

我已经使用了这段代码

- (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);
}

第一次单击按钮图像显示但第二次不显示图像并返回NULL值..给出我们的代码中适用的任何建议和源代码。

3 个答案:

答案 0 :(得分: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开发人员)所以我怀疑这是一个打包问题。

答案 1 :(得分:0)

使用此方法和currentImage = 0:

 -(IBAction)Next
{

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

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

}

答案 2 :(得分:0)

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

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

我认为这会对你有所帮助。

相关问题