开发者

loading an image in a button in iPhone

开发者 https://www.devze.com 2023-04-11 04:54 出处:网络
i\'m new in iPhone app development i\'m developing a game in which involves choosing the right image from four choices. these choices are made up of custom buttons and i have 80 different images which

i'm new in iPhone app development i'm developing a game in which involves choosing the right image from four choices. these choices are made up of custom buttons and i have 80 different images which should be loaded in each button per iteration. I've thought of making 80 different buttons for each image and hiding the 76 buttons while showing only 4 but that would take up a lot of memory. I've also thought of making only four buttons and load the different images there but the only way i know to put an image in a button through code is

[button setImage:image1 forState:UIControlStateNormal];

but using this would also take me a lot of lines for each button.

is there a way for me to load different images in 开发者_Go百科a single button without taking up too much lines in my code?


Yes you can do in a very simple way. Create an array containing the the name of all 80 images. Use

[button setImage:[array objectAtIndex:k] forState:UIControlStateNormal];
k=k+1;

use above code four times as you have four buttons, increament k by each time. start k =0; so in first iteration you k=0, k=1, k=2, k=3 four values and this will continue in second iteration.

0

精彩评论

暂无评论...
验证码 换一张
取 消