how to make 3 labels to appear in random order

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

  1. mandude macrumors member

    Joined:
    Nov 19, 2009
    #1
    ok so i have one round rect button, and when the user taps this button i need one of three labels in the middle of the screen to randomly be chosen to appear and do so. So i make the -(IBAction)buttonPressed; and connect it to the round rect button. Then i write three labels, one saying "Hello" one saying "GoodBye" and the other saying "See you Later" i set the labels check mark to HIDDEN and that how far ive gone. So, in the .M file, where i would type

    -(IBAction)buttonPressed {

    ...............

    }

    what would i need to type in there to make it so when i tap the button, one of the 3 labels is set to label*.hidden = NO randomly. The second time the user taps the button, I also want it to know to choose randomly out of the other two labels that are still hidden, and so on until all the labels are visible. Think of it like choosing a marble from a bag, when you choose one you can't put it back in the bag, so when you press the button it chooses one of three, the second time one of two, and so forth. Is there a way you can do this with an NSArray? make a list... helloLabel, goodByeLabel, seeYouLaterLabel, all help is appreciated
     
  2. ritsard macrumors regular

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #2
    you can use NSArray and store the objects there. You can then generate a value and determine the index of the object in the array and display it. Once it is done, you can then remove it from the array. Then the process repeats for the other two.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    Another way would be to make an array that holds the three labels. Shuffle the array. Then the first time choose the first item. Second time choose the second item. etc.

    See the shuffle algorithm in objective-C on this page:

    http://en.wikipedia.org/w/index.php?title=Fisher–Yates_shuffle&oldid=347141943

    Also, you might just need an array of strings and one label. Just set the title of the label each time to the chosen string.
     

Share This Page