Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Problem with array of images (+ "Change"/"Back" button)

ArthurOffArthurOff Posts: 10Registered Users
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.

-(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];
}


Thank you in advance.
Post edited by ArthurOff on

Replies

  • smithdale87smithdale87 Posts: 4,292iPhone Dev SDK Supporter
    Please read the thread bumping guidelines - http://www.iphonedevsdk.com/forum/iphone-sdk-development/71317-thread-bumping-guidelines.html

    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?
  • QuantumDojaQuantumDoja Posts: 623Registered Users
    Maybe something like this:



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

    www.itheme.com
    deal.gameweaver.com
    guessem.gameweaver.com
  • ArthurOffArthurOff Posts: 10Registered Users
    QuantumDoja;374767 said:
    Maybe something like this:



    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?
  • QuantumDojaQuantumDoja Posts: 623Registered Users
    Now you have a choice on how *should* it work?

    1. When it gets to the end, should i go back down again, then what happens at the end?
    2. add a seperate button and do this instead:



    int ptr = 0; //Put this in your header, just not in the changeImage method
    NSArray *images;

    -(id)init
    {

    images = [[NSArray alloc] initWithObjects:@\"ladybug.jpg\",@\"centipede.jpg\",@\"wolfSpider.jpg\",nil];
    }

    -(IBAction)changeImageForward
    {

    [imageView setImage:[UIImage imageNamed:[images objectAtIndex:ptr]]];

    ptr++;
    if (ptr > [images count])
    {
    ptr = 0;
    }
    }

    -(IBAction)changeImageBack
    {

    [imageView setImage:[UIImage imageNamed:[images objectAtIndex:ptr]]];

    ptr--;
    if (ptr < 0)
    {
    ptr = [images count];
    }
    }

    www.itheme.com
    deal.gameweaver.com
    guessem.gameweaver.com
  • marketshowmarketshow Posts: 91Registered Users
    Create a second array with same images reverse order and use same code.

     NSArray *images = [[NSArray alloc] initWithObjects:@\"wolfSpider.jpg\",@\"centipede.jpg\",@\"ladybug.jpg\",nil];


    PS: I know this is not a good programming way of thinking but its just an array! ;)
    Oroskopos.gr (Lite Version)

    Oroskopos.gr (Full Version)

    iQBubblez

    GO!Enterprise
  • ArthurOffArthurOff Posts: 10Registered Users
    Thanks again!!! Here is my last variant with which I ended this issue:
    -(IBAction)changeImage
    {
    NSArray *images = [[NSArray alloc] initWithObjects:@"ladybug.jpg",@"centipede.jpg",@"wolfSpider.jpg",nil];

    [imageView setImage:[UIImage imageNamed:[images objectAtIndex:i]]];

    i++;
    if (i >= [images count])
    {
    i = 0;
    [changeButton setTitle:@"Back" forState:UIControlStateNormal];
    } else
    {
    [changeButton setTitle:@"Change" forState:UIControlStateNormal];
    }

    [images release];
    }
    QuantumDoja;374934 said:
    Now you have a choice on how *should* it work?

    1. When it gets to the end, should i go back down again, then what happens at the end?
    2. add a seperate button and do this instead:



    int ptr = 0; //Put this in your header, just not in the changeImage method
    NSArray *images;

    -(id)init
    {

    images = [[NSArray alloc] initWithObjects:@\"ladybug.jpg\",@\"centipede.jpg\",@\"wolfSpider.jpg\",nil];
    }

    -(IBAction)changeImageForward
    {

    [imageView setImage:[UIImage imageNamed:[images objectAtIndex:ptr]]];

    ptr++;
    if (ptr > [images count])
    {
    ptr = 0;
    }
    }

    -(IBAction)changeImageBack
    {

    [imageView setImage:[UIImage imageNamed:[images objectAtIndex:ptr]]];

    ptr--;
    if (ptr < 0)
    {
    ptr = [images count];
    }
    }

Sign In or Register to comment.