Iphone ui“的按钮和操作;下一个';和以前的图像

Iphone ui“的按钮和操作;下一个';和以前的图像,iphone,ios,uiimageview,uibutton,ibaction,Iphone,Ios,Uiimageview,Uibutton,Ibaction,所以,对于iPhone开发人员来说,这是一个非常新的概念,并且一直在学习。我知道我想要什么,但不知道从哪里开始。在这个项目之前,我只处理过tableView应用程序,所以我对imageView和自定义操作的实际知识非常缺乏 我想让UIImageView加载一个图像(使用由fetchedResultsController填充的数组,因为图像将存储在CoreData数据库中) 我想要一个动作为“下一步”的UIButton和动作为“上一步”的UIButton,它们将控制前面提到的ImageView显示

所以,对于iPhone开发人员来说,这是一个非常新的概念,并且一直在学习。我知道我想要什么,但不知道从哪里开始。在这个项目之前,我只处理过tableView应用程序,所以我对imageView和自定义操作的实际知识非常缺乏

我想让UIImageView加载一个图像(使用由fetchedResultsController填充的数组,因为图像将存储在CoreData数据库中)

我想要一个动作为“下一步”的UIButton和动作为“上一步”的UIButton,它们将控制前面提到的ImageView显示的图像

到目前为止,我已经有了数据库映射和接口设计,但我没有找到这些问题的答案(我相信这对那些知道的人来说是相当直接的)

任何建议或示例代码都将不胜感激

脱党

@下面的shawnwall代码段

-(IBAction)nextImage { 
    index++; 
    int imageCount=31; 
    if (index<=imageCount) { 
        NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
        [picture setImage: [UIImage imageNamed: imageName]]; 
    } 
} 
正在此处添加到阵列的图像:

Images *imageS = (Images *)[NSEntityDescription insertNewObjectForEntityForName:@"Images" inManagedObjectContext:managedObjectContext];

imageS.topImage = selectedImage;


[topImageArray insertObject:imageS.topImage atIndex:0];

NSLog(@"%i" , [topImageArray count]);
。。。

相同的方法,但在创建缩略图后,我调用:

[self updateTopIV];
即:

-(void)updateTopIV 
{
topIV.image = [topImageArray objectAtIndex:0];
}
然后我有两个操作按钮(下一个/上一个):


在.xib中的视图中放置两个UIButton(如果您使用的是IB)

在视图控制器中创建两个iAction。h:

-(IBAction)nextClicked:(id)sender;
-(IBAction)prevClicked:(id)sender;
m:

-(IBAction)nextClicked:(id)sender
{
  //do stuff
  imageView.image = whatever;
}

-(IBAction)prevClicked:(id)sender
{
  //do stuff
  imageView.image = whatever;
}

接下来,通过IB将这些操作链接到按钮的“内部触摸”事件。这将触发这些事件。

将图像放入一个数组中,并将当前图像的索引放入一个变量中

NSInteger *currentPhotoIndex;

- (IBAction)nextTouch
{
    [self showPhoto:1]; // number to add to current, so 1 for next, -1 for previous
}

- (IBAction)previousTouch
{
    [self showPhoto:-1];
}


// Actual "Logic"

- (void)showPhoto:(NSInteger)numberToAdd
{
    NSArray *arrayOfImages = allTheImagesYouWantToShow
    UIImageView.image = [arrayOfImages objectAtIndex:currentPhotoIndex + numberToAdd];
    // Adding one or subtracting one from the current image's index will show the photo
    immediately before or after it. With this method, you can also skip around to any
    number you want.
}
我认为应该这样做

-- 编辑:

currentPhotoIndex应该像您所做的那样在头文件中定义。它应该设置为显示的第一个图像的数组中的位置。现在,让我们假设它是第一个

第一次显示图像时,请执行以下操作:

currentPhotoIndex = [arrayOfImages indexOfObject:thePhotoI'mShowingRightNow];
还有,接得好。为了防止NSInteger超出数组的边界,您需要以下内容:

- (void)showPhoto:(NSInteger)numberToAdd
{
    if (currentPhotoIndex > 0 && < arrayOfImages.count) // Makes sure that the int is within the array
    {
        // Show new image
    }
}
-(无效)showPhoto:(NSInteger)number添加
{
if(currentPhotoIndex>0&&
shawnwall不错!这是第一步√. 现在有什么想法来为这个行动生成逻辑吗?我发现这似乎符合我的要求,但我不会有静态数量的图像,因为用户可以添加图像
{static int index=0;//只需使用[arrayOfImages count]获取数组计数即可。谢谢你慷慨的输入。如果我能通过这一关,从核心数据存储中获取图像并立即放入可变数组(叹气…)将会让我大吃一惊!这不会发生,伙计:(.我在上面添加了更多的代码片段,以说明我的设置是否有效。我不明白currentPhotoIndex的值是如何定义的(我在h中添加了一个整数。)另外,当下一个和上一个落在数组之外时,你会如何阻止它?忘记接受你的工作答案了,我的坏家伙,谢谢你的建议,它确实起了作用!
currentPhotoIndex = [arrayOfImages indexOfObject:thePhotoI'mShowingRightNow];
- (void)showPhoto:(NSInteger)numberToAdd
{
    if (currentPhotoIndex > 0 && < arrayOfImages.count) // Makes sure that the int is within the array
    {
        // Show new image
    }
}