views:

131

answers:

4

Hi,

I have a UIImageView *picture

and a UIButton *next

and an - (IBAction)next { }

I want to change the image on the view but only if the image equals... for example img1

But using the same button I want to also be able to change the picture if the image = img2 but to a different image (img3)

So far I have this code but it gives me errors please could you help:

Code:

- (IBAction)next {
    if (picture UIImage = imageNamed:@"img01.jpg") {
    [picture setImage: [UIImage imageNamed:@"img02.jpg"
    }
    if (picture UIImage = imageNamed:@"img02.jpg") {
    [picture setImage: [UIImage imageNamed:@"img03.jpg"
 }
}

Please help

Thanks,

Will

A: 
- (IBAction)next {
    if ([[picture image] isEqual: [UIImage imageNamed:@"img01.jpg"]]) {

       [picture setImage: [UIImage imageNamed:@"img02.jpg"]];

    } else if ([[picture image] isEqual: [UIImage imageNamed:@"img02.jpg"]]) {

       [picture setImage: [UIImage imageNamed:@"img03.jpg"]];
    }
}

Also, this is a pretty rudimentary way of changing the image. A more elegant way would be to have a global int that had the current image index, so clicking "next" would simply increase that count. Then, if an image exists with that name, switch to it:

// Declare index in Header.h

index=0;

- (IBAction)next {
    index++;
    // Set imageCount to as many images as are available
    int imageCount=2;
    if (index<=imageCount) {
        NSString* imageName=[NSString stringWithFormat:@"img%02i", index];
        [picture setImage: [UIImage imageNamed: imageName]];
    }
}
pop850
IT just quits the application??
Will
Please could you help it says UIImageView may not respond to UIImage
Will
EDITED- sorry i forgot ending ")" in the if statement. Also, my second example might be more useful for using more than 3 images.
pop850
Sorry how do I declare something?
Will
Please help do I use #define?
Will
In your header file, look after... "@interface MainViewController : UIViewController {"... and add: "int index;"
pop850
Don't use global vars unless there's no alternative, and change the `%i` to `%02i` at least since this will give the OP another headache.
mvds
A: 

Subclass UIImageView and add a enum property in the header:

typedef enum _PictureType {
    PictureTypeFirstImage = 0,
    PictureTypeSecondImage,
    PictureTypeThirdImage,
    PictureTypes
} PictureType;

@interface MyImageView : UIImageView {
    PictureType type;
}

@property (readwrite) PictureType type;

In the implementation:

@synthesize type;

When initializing your picture:

[picture setImage:[UIImage imageNamed:@"img01.jpg"]];
picture.type = PictureTypeFirstImage;

In your action method:

- (IBAction) next {
    switch (picture.type) {
        case (PictureTypeFirstImage): {
            [picture setImage:[UIImage imageNamed:@"img02.jpg"]];
            picture.type = PictureTypeSecondImage;
            break;
        }
        case (PictureTypeSecondImage): {
            [picture setImage:[UIImage imageNamed:@"img03.jpg"]];
            picture.type = PictureTypeThirdImage;
            break;
        }
        default:
            break;
    }
}
Alex Reynolds
error: no type or storage class may be specified here before 'implementation'/Users/Will/Desktop/SUC/Classes/Pictures.m:15:0 /Users/Will/Desktop/SUC/Classes/Pictures.m:15: error: expected identifier or '(' before 'synthesize'/Users/Will/Desktop/SUC/Classes/Pictures.m:41:0 /Users/Will/Desktop/SUC/Classes/Pictures.m:41: fatal error: method declaration not in @interface context
Will
I get those errors please help error: no type or storage class may be specified here before 'implementation' error: expected identifier or '(' before 'synthesize
Will
When you subclass `UIImageView`, the subclass needs its own separate header and implementation files.
Alex Reynolds
Why subclass if you have a `.tag` property for exactly that purpose?
mvds
Can't understand why you would make things so complicated if the OP doesn't even get basic C syntax right. (no offense btw)
mvds
Yeesh, you're right. `UIImageView` has a `tag` property. Of course. I feel stupid. Feel free to ignore my answer.
Alex Reynolds
+1  A: 
- (IBAction)next {
    picture.tag++;
    [picture setImage:[UIImage imageNamed:
             [NSString stringWithFormat:@"img%02d.jpg",1+(picture.tag%2)]
    ]];
}

should be the simplest solution.

edit on first click, goes to img02.jpg, on second click back to img01.jpg again. Increase the 2 to allow for img03.jpg etc.

mvds
Wow, I really overthought my answer...
pop850
+1  A: 

I figured it out now all I did was forget to put .jpg on the end of the img%i ;)

- (IBAction)next {
     static int index = 0;  // <-- here
    index++;
    // Set imageCount to as many images as are available
    int imageCount=16;
    if (index<=imageCount) {
        NSString* imageName=[NSString stringWithFormat:@"img%i", index];
        [picture setImage: [UIImage imageNamed: imageName]];
    }
}
Will
You shouldn't use static variables unless you have a pressing need. You now have no way outside this function to get the image index.
mvds