Search code examples
iphoneiosuiimageviewuibuttonibaction

UIButton and action for "next' and previous image


So, pretty new to iPhone dev and learning all the time. I know what I want but dont quite know where to start. I have really only dealt with tableView apps before this project so my practical knowledge of imageView's and custom actions is very much lacking!

I want to have a UIImageView loaded with an image (using an array populated by a fetchedResultsController as images will be stored in a CoreData DB ).

I want an UIButton with action "next" and UIButton with action "previous" which will control the previously mentioned imageViews' displayed image.

Thus far I have the DB mapped and interface design but am failing to find this answer to these points (I am sure it will be fairly straight forward to those who know).

Any advise or example code would be greatly appreciated.

DetartrateD

@shawnwall code snippet from below...

-(IBAction)nextImage { 
    index++; 
    int imageCount=31; 
    if (index<=imageCount) { 
        NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
        [picture setImage: [UIImage imageNamed: imageName]]; 
    } 
} 

Mutatable array seeded in viewDidLoad:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Images"      inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"topImage" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSError *error = nil;
NSMutableArray *mutableFetchResultsT = [[managedObjectContext executeFetchRequest:request error:&error]mutableCopy];
if (mutableFetchResultsT == nil) {
}

[self setTopImageArray:mutableFetchResultsT];

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

Image being added to array here:

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

imageS.topImage = selectedImage;


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

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

... ...

Same method but after I have created a thumbnail i call:

[self updateTopIV];

Which is :

-(void)updateTopIV 
{
topIV.image = [topImageArray objectAtIndex:0];
}

I then have the 2 action buttons (next/previous):

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

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


 - (void)showPhoto:(NSInteger)numberToAdd
{
    topIV.image = [topImageArray objectAtIndex:currentPhotoIndex + numberToAdd];

}

Solution

  • Put the images in an array, and have the current image's index in a variable

    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.
    }
    

    I think that should do it.

    -- EDIT:

    currentPhotoIndex should be defined in your header file as you have done. It should be set to the position in the array of the first image you show. For now, lets assume it's the first one.

    The first time that you show an image, do this:

    currentPhotoIndex = [arrayOfImages indexOfObject:thePhotoI'mShowingRightNow];
    

    Also, good catch. In order to prevent the NSInteger from being out of the array's bounds, you need something like this:

    - (void)showPhoto:(NSInteger)numberToAdd
    {
        if (currentPhotoIndex > 0 && < arrayOfImages.count) // Makes sure that the int is within the array
        {
            // Show new image
        }
    }