so have array, list of of 2 labels, "hellolabel", , "goodbyelabel" need able randomly select object array, in case 1 of 2 labels, , make them become visible, because in interface builder 2 labels checked hidden please :[
code:
blah.h uilabel *hellolabel; uilabel *goodbyelabel; @property (nonatomic, retain) iboutlet uilabel *hellolabel; @property (nonatomic, retain) iboutlet uilabel *goodbyelabel; -(ibaction)buttonpressed; @end blah.m -(ibaction)buttonpressed { nsarray *array = [[nsarray alloc] initwithobjects: hellolabel, goodbyelabel, nil]; //* select random object nsarray "array" //*set random object *label.hidden = no; //* how how how do [array release];
use array "objectatindex" access object in array list, in case 1 of label objects, set it's property, "sethidden" "no" hide it.
feed "objectatindex" integer "array objectatindex:2" show object array, in case want make random number, use "random() % x" function finds random number, x limit. in case, want use number "2" since have objects in array.
a better way, instead of hard coding number 2 in random() function find the number of objects in array, using [array count] , use number instead. way if add more objects (more labels) array, won't have readjust random() function.
makes code more flexible , causes less potential bugs in future (like if change array size/number of items).
code:
//* set random number range max number of objects // array using [array count] [[array objectatindex: (random() % [array count])] sethidden: no];
note there alternative ways of producing more true (less pseudo-)random numbers, won't go here.
Forums iPhone, iPad, and iPod Touch iOS Programming
Comments
Post a Comment