Create a list
listOfButtons will be used to store all of the button components in your app.
Add buttons to your list
There are a couple of ways you might do this. The first is to add each button when the screen opens. This approach will only work if your UI contains all the buttons you already need.
If however, you are creating your buttons dynamically, you can add each new button, one at a time, to the end of your
listOfButtons with something like this:
Which button was clicked?
In this scenario, we’re going to create a function that accepts one input and returns one output. In the example below:
- The function is called
- The input is called
- The output is a new variable we have created called
The function goes through the
listOfButtons one at a time, to see if it corresponds to the
clickedButton. If it does the value of
positionInList is returned, but if it doesn’t match then the
positionInList is increased by one and the checking happens again.
Calling the function
Finally, when any button is clicked, you can tell which which button was clicked by calling the
findPositionInList block like so: