I have 'n' button. I want to shuffle those button in my application. Or you can say that i want to shuffle the title over buttons. Is it possible.Its an Iphone app.
A:
Of course it's possible. You can swap titles on two buttons like this:
UIButton *b1, *b2;
NSString *tmp;
tmp = [b1 text];
[b1 setText:[b2 text]];
[b2 setText:tmp];
You can extend this procedure to n buttons.
Pablo Santa Cruz
2010-08-05 13:47:34
yah it is possible to swap the text.But suppose i have 100 buttons then it won't help. So, What you can do if this is the scenario in front of you? So, find that there is any shuffle methods are available, which can be used to shuffle whole button!!
Tauquir
2010-08-05 13:56:19
Just loop through all of the buttons then and swap at random
iWasRobbed
2010-08-05 14:12:12
Who would want to use an application with 100 shuffling buttons?!
dreamlax
2010-08-05 21:25:35
this piece of code is not working correctly. I am using third button, on click this button the text over two button swap. Can you have more elaborate on this topic. thnks
Tauquir
2010-08-06 13:39:07
+1
A:
What you might need to know:
- How to change the position of a button. You can do this with
frame
property. If you change just theorigin
member ofCGRect
, you can move the button without resizing it. - How to change the title of a button (if you don't want to change its position). This can be achieved with the
setTitle:forState:
method ofUIButton
. - Determining a random number. For general-purpose random number generation, you can use the
rand()
method. For more serious random number generation there are other methods available butrand()
should suffice for your needs. Just ensure to callsrand()
with some seed before your first call torand()
. - If you have 100 buttons, you probably should not use Interface Builder but you should create the buttons separately, otherwise you will end up with a class with 100
IBOutlet
variables. Creating them yourself and maintaining them in an array will give you greater control over their shuffling. Creating them manually has been discussed on StackOverflow previously.
dreamlax
2010-08-06 00:38:34