在将显示图像(以数组形式)并具有连接到各自的下一个和上一个方法的下一个和上一个按钮的视图中。
如何创建用于 objectAtIndex:counter 的计数器?
数组中图片导航的要求是:
- 点击下一步 -> 下一张图片
- 点击上一张 -> 上一张图片
- 单击下一步,它的最后一张图片 - > 转到第一张图片
- 单击上一张及其第一张图片->转到最后一张图片
- 没有图片(第一次运行和创建之前)-> 什么都不做。
您可以轻松地做到这一点您需要为该类创建一个全局变量(计数),然后执行此操作。
-(IBAction)previousImage:(id)sender
{
if(count==0)
{
count=array.count-1;
imageView.image = [UIImage imageNamed:[array objectAtIndex:count]];
}
else
{
count--;
imageView.image = [UIImage imageNamed:[array objectAtIndex:count]];
}
}
-(IBAction)nextImage:(id)sender
{
if(count==array.count-1)
{
count=0;
imageView.image = [UIImage imageNamed:[array objectAtIndex:count]];
}
else
{
count++;
imageView.image = [UIImage imageNamed:[array objectAtIndex:count]];
}
}
根据上面的答案和评论实现的代码(我使用一个 singelton 和 coreData 来保存图像):
static NSUInteger count;
- (IBAction)onNext{
DataControllerSingleton *singletonData= [DataControllerSingleton singleDataController];
NSInteger total = [singletonData countOfList];
self.cares = [singletonData allObjects];
if(count == total-1)
{
count=0;
NSObject *ob = [cares objectAtIndex:count];
self.backgroundImage.image = [[UIImage alloc]initWithData:[ob
valueForKey:@"imageData"]];
}
else
{
count++;
NSObject *ob = [cares objectAtIndex:count];
self.backgroundImage.image = [[UIImage alloc]initWithData:[ob
valueForKey:@"imageData"]];
}
}
- (IBAction)onPrevious{
DataControllerSingleton *singletonData= [DataControllerSingleton singleDataController];
NSInteger total = [singletonData countOfList];
self.cares = [singletonData allObjects];
if(count==0)
{
count=total-1;
NSObject *ob = [cares objectAtIndex:count];
self.backgroundImage.image = [[UIImage alloc]initWithData:[ob
valueForKey:@"imageData"]];
}
else
{
count--;
NSObject *ob = [cares objectAtIndex:count];
self.backgroundImage.image = [[UIImage alloc]initWithData:[ob
valueForKey:@"imageData"]];
}
}
这里的总数是图像的数量。
-(IBAction)previous:(id)sender{
if(count==0)
count=total-1;
else
count--;
}
-(IBAction)next:(id)sender{
if(count==total-1)
count=0;
else
count++;
}