How to select random label from an array

Discussion in 'iOS Programming' started by mandude, Apr 25, 2010.

  1. mandude macrumors member

    Nov 19, 2009
    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 :[

    UILabel *helloLabel;
    UILabel *goodByeLabel;
    @property (nonatomic, retain) IBOutlet UILabel *helloLabel;
    @property (nonatomic, retain) IBOutlet UILabel *goodByeLabel;
    -(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;
    [array release];
  2. flyingturtle macrumors regular

    Apr 7, 2010
    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.

    //*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).

    //* 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.
  3. mandude thread starter macrumors member

    Nov 19, 2009
    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...
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    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.
  5. mandude thread starter macrumors member

    Nov 19, 2009
    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

    UIImageView *view1;
    UIImageView *view2;
    UIImageView *view3;
    NSMutableArray *array;
    m. file

    -(void)viewDidLoad {
    array = [[NSMutableArray alloc] initWithObjects: view1, view2, view3 ,nil];
    -(IBAction)buttonPressed {
    	[[array objectAtIndex: (random() % [array count])?????????];
    CGPoint point1 = **// RANDOM IMAGEVIEW FROM;
    point1.x = 50;
    point1.y = 100;
    **// RANDOM IMAGEVIEW FROM = 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)

Share This Page