Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-15-2011, 11:48 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: Newcastle upon Tyne, Tyne and Wear, United Kingdom
Age: 20
Posts: 10
ArthurOff is on a distinguished road
Send a message via Skype™ to ArthurOff
Question 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];
}
Thank you in advance.
ArthurOff is offline   Reply With Quote
Old 09-15-2011, 03:57 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: Newcastle upon Tyne, Tyne and Wear, United Kingdom
Age: 20
Posts: 10
ArthurOff is on a distinguished road
Send a message via Skype™ to ArthurOff
Default

Anyone?
ArthurOff is offline   Reply With Quote
Old 09-15-2011, 03:58 PM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Please read the thread bumping guidelines - http://www.iphonedevsdk.com/forum/ip...uidelines.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?
smithdale87 is offline   Reply With Quote
Old 09-15-2011, 04:01 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

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;
    }
}
QuantumDoja is offline   Reply With Quote
Old 09-16-2011, 06:33 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: Newcastle upon Tyne, Tyne and Wear, United Kingdom
Age: 20
Posts: 10
ArthurOff is on a distinguished road
Send a message via Skype™ to ArthurOff
Default

Quote:
Originally Posted by QuantumDoja View Post
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?
ArthurOff is offline   Reply With Quote
Old 09-16-2011, 06:43 AM   #6 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

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:

Code:
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];
    }
}
QuantumDoja is offline   Reply With Quote
Old 09-16-2011, 06:47 AM   #7 (permalink)
myCustomUserTitle
 
marketshow's Avatar
 
Join Date: Dec 2010
Location: Athens
Age: 27
Posts: 79
marketshow is on a distinguished road
Default

Create a second array with same images reverse order and use same code.

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!
marketshow is offline   Reply With Quote
Old 09-16-2011, 06:52 AM   #8 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: Newcastle upon Tyne, Tyne and Wear, United Kingdom
Age: 20
Posts: 10
ArthurOff is on a distinguished road
Send a message via Skype™ to ArthurOff
Default

Thanks again!!! Here is my last variant with which I ended this issue:

Quote:
-(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];
}
Quote:
Originally Posted by QuantumDoja View Post
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:

Code:
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];
    }
}
ArthurOff is offline   Reply With Quote
Reply

Bookmarks

Tags
apple, ios, mac, os x, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 407
16 members and 391 guests
7twenty7, Eclectic, eski, EvilElf, fiftysixty, HemiMG, iOS.Lover, JackReidy, jarv, Pudding, sacha1996, teebee74, tim0504, UMAD, VinceYuan, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:10 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0