سؤال

I have my UIPickerWheel setup to spin by a flick and populate a UILabel with whatever word value it lands on and that works just fine.

This issue that I'm having is when I added a button that spins my picker wheel and randomly chooses a word value. It spins the wheel just fine, and it populates my UILabel as well, but the UILabel and what is showing on the picker wheel doesn't match. I'm going to paste the code below.

I know that the way I have it setup right now is basically saying when user presses the button, spin the wheel and select a random value, and set the Text to label for the selectedItem. Right now the selectedItem = ? is where I'm having the issue. if I enter a string (e.g. @"hello") the label will say "hello" no matter what is lands on and if I type a number value of 0-5610 (the words on my array) then it will show that word.

Right now I have it just randomly choose a word. So I know that issue is on that line.

The line where im assuming my issue is coming from would be:

 selectedItem = 

/// HERE'S THE CODE///

 - (IBAction)spin:(id)sender {

[pickerView selectRow:(arc4random() % [self pickerView:pickerView numberOfRowsInComponent:0]) inComponent:0  animated:YES];

selectedItem = [words objectAtIndex:(arc4random() % [words count])];

[label setText:selectedItem];     

}
هل كانت مفيدة؟

المحلول

You are creating two different random numbers. You set the picker with one and choose a word from the word list with the other. Instead, create one random number. Use it for both operations:

- (IBAction)spin:(id)sender {
    NSUInteger number = arc4random_uniform(words.count);
    [pickerView selectRow:number inComponent:0 animated:YES];

    selectedItem = words[number];
    label.text = selectedItem;     
}

This assumes the words in the picker match the words in the words array.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top