user1418214
user1418214

Reputation: 59

pass parameter from one IBAction to another

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

Answers (2)

Mr.Anonymous
Mr.Anonymous

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

David Hoerl
David Hoerl

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

Related Questions