0

For my education I build to build a dictionary app which is readable through picker view. It has basically a dutchwords array and a English words array.

And I can ad words as well. This is all working. And i though i could give the user the correct throw their indexes.

//selected by picker view
if englishDic.index == dutchDic.index{ 
    //correct answer
}
else{
    //wrong answer
}

It doesn't work this way.

1
  • 3
    maybe post some more code? Commented Jan 3, 2016 at 0:41

1 Answer 1

1

Arrays don't have an index property that you can access like that. What you probably want to do is capture the selected index for each picker in the Picker View's didSelectRow function in the UIPickerViewDelegate.

Once you've stored those in variables, you can compare the selected indexes.

Sign up to request clarification or add additional context in comments.

5 Comments

It's important to note that the arrays are the 'data source' for the picker view. The array doesn't 'know' that it is being used by a picker view, so updating the selection in the picker view has no impact at all on the array.
@IBAction func checkTranslationButton(sender: AnyObject) { func pickerView(pickerview: UIPickerView, didSelectRow: Int, inComponent: Int) { var action1 = pickerView(<#T##pickerView: UIPickerView##UIPickerView#>, didSelectRow: 0, inComponent: 0) var action2 = pickerView(<#T##pickerView: UIPickerView##UIPickerView#>, didSelectRow: 0, inComponent: 1) } The errors i get is that i can't get the int from the rows. So I'm stuck. Sorry to bother you again.
The didSelectRow functions will get called when a user selects a row (as the name implies.) These shouldn't be nested in your 'button' function. You need to... 1 - create variables in your ViewController to store the selected index for each picker. 2 - Update these variables using the didSelectRow functions. 3 - Compare the variables in your checkTranslationButton function.
If you're going to get into iOS development, it's really important to be across the Delegation Pattern. I can see that this is the concept you are struggling with here.
And that is my basic problem as well. how can i make the two variations to select the two rows/index from the picker view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.