i have a question view and will show 4 answers, only 1 are correct.
but i do not want the same button to be always the correct answer.
i will like to know how do i do a random placement of 4 UIButton and value every time.
when user go to this question again the answer will be in differ button
my placement of X,y,W,H
button 1 5,219,230,45
button 2 5,273,230,45
button 3 244,224,230,45
button 4 244,273,230,45
create your own class with these 4 buttons. In this class you can store the positions of your buttons. then you can add a member function (something like the following) which will draw them:
I would suggest do not change the position of buttons rather what you can do is change the
currentTitle
property of the buttons randomly and once the user clicks the button ; you can get thecurrentTitle
of the button and check if it is the correct answer or not.Out of 4 options only one is correct so why you worry, just give tag to your UIButton. In touchUpInside event or any action you've specified on it. Just match the correct answer tag with user touched button tag.
You should set some logic to set answer with different button tag randomly.
You can create the buttons in same position always and randomize the labels on the button. If you are using an array to provide labels like val1, val2, val3 etc, you can randomize the array.
I have implemented code for same at my side and it works fine.
You should create button and add them as
subView
. When you need to put button at random location, you can use: