Display random text from a custom word bank?

Discussion in 'iPhone/iPad Programming' started by howirocks, Nov 2, 2009.

  1. macrumors newbie

    Joined:
    May 7, 2008
    #1
    Bare with me here...

    I'm working on a name generator. I want to use (i think) an NSArray of words to serve as a word bank to randomly pick from. It would be 3 separate words to make a funny random name like "Slippery California Farmers" or something.

    So I created and positioned a UILabel for each of the 3 words and used:

    - (IBAction)buttonClick:(id)sender {
    NSString *title = nil;
    NSString *path = nil;


    int Number = rand() % 3;
    switch (Number) {

    case 0:
    myTextLabel1.text = @"Slippery";
    myTextLabel2.text = @"California";
    myTextLabel3.text = @"Farmers";
    break;

    case 1:
    myTextLabel1.text = @"Raisin";
    myTextLabel2.text = @"Hell";
    myTextLabel3.text = @"Dudes";
    break;

    case 2:
    myTextLabel1.text = @"Abnormal";
    myTextLabel2.text = @"Scissor";
    myTextLabel3.text = @"Killers";
    break;

    }

    It shows up fine. But how would I randomize to change the text for each label to pick a different word, creating a new name each time?

    And could I create my own invisible word bank to pick from?

    Hope you understand what I'm trying to say. I'm new to developing.

    Thanks in advance.
     
  2. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #2
    You can populate an NSArray a variety of ways. Here's one:
    Code:
     
    [COLOR=#400080]NSArray[/COLOR] [COLOR=#002200]*[/COLOR] firstList [COLOR=#002200]=[/COLOR] [COLOR=#002200][[/COLOR][COLOR=#002200][[/COLOR][COLOR=#400080]NSArray[/COLOR] alloc[COLOR=#002200]][/COLOR] initWithObjects[COLOR=#002200]:[/COLOR][COLOR=#bf1d1a]@[/COLOR][COLOR=#bf1d1a]"Slippery"[/COLOR],[COLOR=#bf1d1a]@[/COLOR][COLOR=#bf1d1a]"Raisin"[/COLOR],[COLOR=#bf1d1a]@[/COLOR][COLOR=#bf1d1a]"Abnormal"[/COLOR],[COLOR=#a61390]nil[/COLOR][COLOR=#002200]][/COLOR];
    
    You can have similar collection for the other two parts of the name.

    Then fetch each part of the String based on index (position) in the array using the objectAtIndex: method -

    Code:
     
    [firstList objectAtIndex:Number]
    
     
  3. thread starter macrumors newbie

    Joined:
    May 7, 2008
    #3
    Right on, thanks. I'll give this shot and reply back!
     
  4. thread starter macrumors newbie

    Joined:
    May 7, 2008
    #4
    Code:
    NSArray *adjList = [[NSArray alloc] initWithObjects:@"Slippery",@"Red",@"Rasta",@"Natty",nil];
        [adjList objectAtIndex:0;]      <== ON THIS LINE  - Expected ']' before ';' token
        [*adjList release];         <==AND THIS LINE - Expected ']' before ';' 'release'
    I used @synthesize above it:

    Code:
    @synthesize myButton, myTextLabel1, myTextLabel2, myTextLabel3;
    @synthesize adjList;
    @synthesize verbList;
    @synthesize nounList;
    What am I doing wrong?
     
  5. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #5
  6. thread starter macrumors newbie

    Joined:
    May 7, 2008
    #6
    Awesome, the errors went away. I appreciate your help. I am definitely learning as I go. I keep forgetting things I've done and read.

    If you don't mind, I just need another pointer. Should I link the specific text label outlets to the respected NSArrays I want them to draw from?
     
  7. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #7
    No. Create the outlets in your view controller, and have your button call an IBAction method that will generate the random number(s) and change the labels accordingly.
     

Share This Page