0

I made 26 buttons, A-Z and when I click them the image of the button I pressed will display in an image view, when I click A, it displays A, When I click B it will display B etc.

The problem I have is that they won't display next to each other, I have 6 image views and I want the first letter to fill first box second letter second box etc.

I just don't know how to do this, I've been trying a lot of ways now and i'm pretty sure i'll need to use arrays just can't get the code right.

This is a picture of where I am now I'm already past number 1 so it's like number 2 except for showing next each other ofc.

hope someone can tell me here is the picture:

http://img213.imageshack.us/img213/4500/questionow.png

thanks

This is what I got now .h

    IBOutlet UIImageView *imageview1;
    IBOutlet UIImageView *imageview2;
    IBOutlet UIImageView *imageview3;
    IBOutlet UIImageView *imageview4;
    IBOutlet UIImageView *imageview5;
    IBOutlet UIImageView *imageview6;
}

-(IBAction)showA;
-(IBAction)showB;
-(IBAction)showC;
-(IBAction)showD;
-(IBAction)showE;
-(IBAction)showF;
-(IBAction)showG;
-(IBAction)showH;
-(IBAction)showI;
-(IBAction)showJ;
-(IBAction)showK;
-(IBAction)showL;
-(IBAction)showM;
-(IBAction)showN;
-(IBAction)showO;
-(IBAction)showP;
-(IBAction)showQ;
-(IBAction)showR;
-(IBAction)showS;
-(IBAction)showT;
-(IBAction)showU;
-(IBAction)showV;
-(IBAction)showW;
-(IBAction)showX;
-(IBAction)showY;
-(IBAction)showZ;

.m

-(IBAction)showA {

UIImage *img = [UIImage imageNamed:@"A.png"];

[imageview1 setImage:img];

}

-(IBAction)showB {

UIImage *img = [UIImage imageNamed:@"B.png"];

[imageview1 setImage:img];  

}

Etc.

Hope someone can tell me how to make this or has some sample code for me because I don't quite get the arrays even after seeing a lot of tutorials on them.

thanks Don

Don Kooijman
  • 593
  • 1
  • 4
  • 13

2 Answers2

1

You could always do:

- (void)setNextImage:(UIImage *)image {
    if (imageview1.image == nil) {
        [imageView1 setImage:image]
    } else if (imageView2.image == nil){
        [imageView2 setImage:image]
    } // ... Through all 6 images
}

and then in each of your button actions just call:

[self setNextImage:img];

instead of setting it directly.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • Would make even more sense to put the imageView pointers into an array and index the array to pick which one. – Hot Licks Apr 17 '12 at 20:35
  • Oke thanks both of you, got a question when I would do it this way is it any bad? because then I will use your way hot licks but If it works and it's alright then It's fine for me. but I don't really know what you mean but I'll look it up then. Heard from 2-3 guys that It was possible with an array so've been struggling with that all day although inafzigers way was what I was thinking myself at first let me give it a try! thanks – Don Kooijman Apr 17 '12 at 20:53
  • With only 6 items I think that this is fine. Much more than that and I would definitely say to use an array. In this case though I think that either way is a valid approach. – lnafziger Apr 17 '12 at 20:55
  • Hey guys thanks so much it's working! really nice so good to see something finally work couldn't have done it without you! :D:D only 1 thing, when I run it from Xcode it would breakdown when I clicked the second image, breakpoint 1.1 or something it would say in xCode only when I press stop in Xcode, then return to the simulator and run the app again then it works perfectly fine. how come? – Don Kooijman Apr 17 '12 at 21:06
  • Oke thanks ya the words in my game can only be 6 characters long maybe expand it to 7 in the future but I'm guessing that isn't such a problem thanks man for the help! – Don Kooijman Apr 17 '12 at 21:07
0

Using an array is not hard. Just declare either a standard C array of UIImageView* or an NSArray, inited like: [NSArray arrayWithObjects:imageView1, imageView2 ... imageView6, nil].

Then have a position counter pos declared at the instance level, and you simply do:

[imageViewArray objectAtIndex:pos].image = imageParm; pos++;

(Or, for the C array, imageViewArray[pos].image = imageParm; pos++;.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • I'll try this out tomorrow just so I know how it works thank you do you know why It breakdown now when I hit the second letter but when I stop Xcode and go back to the simulator, run the app again it's working fine? when Xcode opens it makes the } else if (imageView2.image == nil){ [imageView2 setImage:image] green line and then says breakdown – Don Kooijman Apr 17 '12 at 21:33
  • What do you mean "breakdown"? If you mean "breakpoint" then likely you've set one at that location -- there'd be a sort of arrow marker in the left margin. – Hot Licks Apr 17 '12 at 21:58
  • how come? I mean it runs when I stop Xcode but I don't think it's alright hehe – Don Kooijman Apr 17 '12 at 22:09
  • Guess not hèhè pretty New to this – Don Kooijman Apr 18 '12 at 05:12
  • Nevermind I found it thanks for all your help Guys ill try your arrays out when I get back from work thanks Guys! – Don Kooijman Apr 18 '12 at 05:17