PDA

View Full Version : Display random text from a custom word bank?




howirocks
Nov 2, 2009, 03:42 PM
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.



jeremy.king
Nov 2, 2009, 04:59 PM
You can populate an NSArray a variety of ways. Here's one:

NSArray * firstList = [[NSArray alloc] initWithObjects:@"Slippery",@"Raisin",@"Abnormal",nil];


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 -


[firstList objectAtIndex:Number]

howirocks
Nov 2, 2009, 08:21 PM
Right on, thanks. I'll give this shot and reply back!

howirocks
Nov 2, 2009, 10:04 PM
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:

@synthesize myButton, myTextLabel1, myTextLabel2, myTextLabel3;
@synthesize adjList;
@synthesize verbList;
@synthesize nounList;

What am I doing wrong?

jeremy.king
Nov 2, 2009, 10:43 PM
Might be better to start here - http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html

; goes at end of statement, not inside []
when you use an variable, you don't have to access it with the * notation

howirocks
Nov 2, 2009, 11:24 PM
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?

jeremy.king
Nov 3, 2009, 10:33 AM
Should I link the specific text label outlets to the respected NSArrays I want them to draw from?

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.