Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mandude

macrumors member
Original poster
Nov 19, 2009
64
0
so i have an array, which is a list of of two labels, "helloLabel", and "goodByeLabel" i need to be able to randomly select an object from my array, in this case one of the two labels, and make them become visible, because in interface builder the two labels are checked with HIDDEN please help :[


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 from the NSArray  "array"   

//*set that random object to  *label.hidden = NO;

//* HOW HOW HOW DO I DO THAT

[array release];
 
Use the Array "objectAtIndex" to access an object in your array list, in this case one of your label objects, then set it's property, "setHidden" to "NO" to hide it.

Normally you feed "objectAtIndex" an integer like "array objectAtIndex:2" to show an object from your array, but in this case you want to make a random number, so use the "random() % X" function which finds a random number, where X is the limit. In this case, you want to use the number "2" since you only have to objects in your array.

Code:
//*set that random object to  *label.hidden = NO;

[[array objectAtIndex: (random() % 2)] setHidden: NO];

A better way, instead of hard coding the number 2 in your random() function is to find the the number of objects in your array, by using [array count] and use that number instead. This way if you add more objects (more labels) to your array, you won't have to readjust the random() function.

It makes your code more flexible and causes less potential bugs in the future (like if you change your array size/number of items).

Code:
//* set the random number range to the max number of objects 
// your array by using [array count]

[[array objectAtIndex: (random() % [array count])] setHidden: NO];

Note there are some alternative ways of producing more true (less pseudo-)random numbers, but I won't go into that here.
 
THANK YOU VERY MUCH FOR YOUR HELP but.... lets say i know have three labels in this....... how can i delete one of the labels from the array after I've selected it and set it to SETHIDDEN:NO because once the first label is chosen, i want the second time pressing the button to choose between the remaining two labels, and so on...
 
Look into NSMutableArray as well as its instance methods. See if there is one that could be used to remove an object from the array.
 
no problem i got it [array removeObject:helloLabel];


but now im switching from labels and hiding them to imageViews and moving them. I want to have an array, have a list of three imageView's in the array, randomly select one, and create a CGPoint that will help me move the imageView to its new point. here's what i got so far :


h. file

Code:
UIImageView *view1;
UIImageView *view2;
UIImageView *view3;


NSMutableArray *array;

@property.....
@property.....
@property.....
-(IBAction)buttonPressed;

m. file

Code:
-(void)viewDidLoad {

array = [[NSMutableArray alloc] initWithObjects: view1, view2, view3 ,nil];

}

-(IBAction)buttonPressed {

	[[array objectAtIndex: (random() % [array count])?????????];
CGPoint point1 = **// RANDOM IMAGEVIEW FROM ARRAY.center;
point1.x = 50;
point1.y = 100;
**// RANDOM IMAGEVIEW FROM ARRAY.center = point1;


as you can see i need to be able to point the the random object from the array and set it to that point, (50, 100)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.