Reputation: 59
The code below displays a random word from an array (generateWord), and then accepts a letter as input, based on which button was pressed. I would like, in letterGuessed, to be able to search for the input letter within the contents of the randomWord variable. I know how I would search, but I cannot access the variable. What is the best way to achieve this?
- (IBAction)generateWord:(id)sender {
int arraycount=[wordList count];
int indexchoice=(arc4random() % arraycount);
NSString *randomWord = [wordList objectAtIndex:indexchoice];
self.wordResult.text=[NSString stringWithFormat:@"word: %@ ",randomWord];
}
- (IBAction)letterGuessed:(id)sender {
NSString *letter=[sender titleForState:UIControlStateNormal];
[sender setEnabled:NO];
}
Upvotes: 0
Views: 190
Reputation: 820
2 ways..
1)Have a global copy of the randomVariable and update it every time generateWord is clicked..
2)Or You can circumvent the above approach by simply getting the string contents from wordResult textview/textfield u have created
NSString *randomString = self.wordResult.text
Upvotes: 1
Reputation: 41642
You make "NSString *randomWord" an ivar, so in "generateWord:" you set randonWord (i.e. "randomWord = [wordList objectAtIndex:indexchoice];"), then randomWord is now available when "letterGuessed:" is invoked.
Upvotes: 1