Location: Newcastle upon Tyne, Tyne and Wear, United Kingdom
Age: 20
Posts: 10
Problem with array of images (+ "Change"/"Back" button)
Hello everybody,
I have a problem with a view base application here. Basically, the main idea is showing three images one after another when you push button "Change". Moreover, when it comes to the end, button's label changes to "Back" and when you push that button again pictures start going backwards and when it comes to the first picture, button's label restores to "Change".
Here I tried to code action, however it does not work. It shows only the "Change" button but when you push it, nothing happens. I would like to ask you could you help me and try to find out the possible solution to this problem.
Code:
-(IBAction)changeImage
{
NSArray *images = [[NSArray alloc] initWithObjects:@"ladybug.jpg",@"centipede.jpg",@"wolfSpider.jpg",nil];
static int i = 0;
if (i)
{
for (int i = 0; i < [images count]; i++ )
{
[imageView setImage:[UIImage imageNamed:[images objectAtIndex:i]]];
}
}
if (i == 2)
{
[changeButton setTitle:@"Back" forState:UIControlStateNormal];
i--;
[imageView setImage:[UIImage imageNamed:[images objectAtIndex:i]]];
}
[images release];
}
FYI your code has some serious logic issues.
For instance, what do you think is supposed to happen in your for loop when you call setImage for every image in your array for the same imageView?
int ptr = 0; //Put this in your header, just not in the changeImage method
-(IBAction)changeImage
{
NSArray *images = [[NSArray alloc] initWithObjects:@"ladybug.jpg",@"centipede.jpg",@"wolfSpider.jpg",nil];
[imageView setImage:[UIImage imageNamed:[images objectAtIndex:ptr]]];
ptr++;
if (ptr > [images count])
{
ptr = 0;
}
}
Location: Newcastle upon Tyne, Tyne and Wear, United Kingdom
Age: 20
Posts: 10
Quote:
Originally Posted by QuantumDoja
Maybe something like this:
Code:
int ptr = 0; //Put this in your header, just not in the changeImage method
-(IBAction)changeImage
{
NSArray *images = [[NSArray alloc] initWithObjects:@"ladybug.jpg",@"centipede.jpg",@"wolfSpider.jpg",nil];
[imageView setImage:[UIImage imageNamed:[images objectAtIndex:ptr]]];
ptr++;
if (ptr > [images count])
{
ptr = 0;
}
}
Thanks very much, that really is the solution, however I still cannot figure out how to make them go in reverse order, should I use "prt--" instead?