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

Ios 单击“下一步”按钮时如何逐个显示图像?,ios,objective-c,Ios,Objective C,我在数组中有图像,但它返回空值 我用过这个密码 - (void)viewDidLoad { [super viewDidLoad]; images= [[NSArray arrayWithObjects: [UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], [U

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

我用过这个密码

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

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

在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");
}
您的iAction方法应该有一个sender参数,不是吗

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

您的代码在我看来还行,但我是Mac开发者,因此我怀疑这是打包问题。

在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");
}
您的iAction方法应该有一个sender参数,不是吗

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

您的代码在我看来还行,但我是Mac开发者,因此我怀疑这是打包问题。

使用此方法,currentImage=0:

 -(IBAction)Next
{

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

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

}

使用此方法,并且currentImage=0:

 -(IBAction)Next
{

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

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

}

除了打印currentImage之外,您的代码与您编写的代码完全相同,因为currentImage是整数类型,但您在NSLog中提到了%@字符串类型。这就是它显示错误的原因。您只需按如下所示更改该选项

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

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

除了打印currentImage之外,您的代码与您编写的代码完全正确,因为currentImage是整数类型,但您在NSLog中提到了%@字符串类型。这就是它显示错误的原因。您只需按如下所示更改该选项

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

我想这会对你有帮助。

这会导致一个例外;测试应为:ifcurrentImage>=[images count]。这将导致异常;测试应为:ifcurrentImage>=[图像计数]。