Quote:
Originally Posted by chris Good
have a view and you're setting the view to contain a randomly selected image when the user presses one button, and you want the view to go back to the previous image when they press the other button
//this button displays images in the array randomly in no order
-(IBAction)nextbutton  id)sender;
{
int ptr = rand() % 6;
NSArray *images = [[NSArray alloc] initWithObjects:@"Americans.png",@"Approach.png",@ "Arianny.png",@"Atoms.png",@"Australia.png",@"Aver age.png",nil];
}
//I want to make a button that will display the previous image of the images in array above
|
The code you posted does not load an image, or install it into a view. It doesn't even select a random image name from your array.
Do you have more complete code for what you're trying to do, or is this as far as you've gotten?
What you need to do is this:
Create a new NSMutableArray as an instance variable, and initialize it to an empty array in your viewDidLoad method. Lets call it previousImageIndexes.
In your next button code, do the following:
- Get a random index.
- Use objectAtIndex to extract a filename from your array of filenames.
- Use UIImage imageNamed to load that image.
- Install that image into your image view.
- Create an NSNumber out of your random index using NSNumber numberWithInteger.
- Add the NSNumber to your previousImageIndexes.
In your back button method, get the last item in the previousImageIndexes array of NSNumbers. (There is an NSArray method lastObject to do that for you)
Get the integer value of that entry from previousImageIndexes, and use it to index into your array of image names.
Just like you did for your next button, load the image and install it in your image view.
Finally, delete the NSNumber object from the previousImageIndexes array using removeObject or removeObjectAtIndex.