gpt4 book ai didi

ios - 单击下一步按钮时如何显示一张图像?

转载 作者:行者123 更新时间:2023-12-01 16:56:29 25 4
gpt4 key购买 nike

我在数组中有图像,但它返回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值。.给出适用于我们代码的任何建议和源代码。

最佳答案

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开发人员),所以我怀疑这是包装问题。

关于ios - 单击下一步按钮时如何显示一张图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11360717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com