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.
Asked
Active
Viewed 296 times
0
-
2Of course it's possible. – Pablo Santa Cruz Aug 05 '10 at 13:45
-
possible duplicate of [i am using 5 buttons in the cocoa interface builder and i want to shuffle it.](http://stackoverflow.com/questions/3404218/i-am-using-5-buttons-in-the-cocoa-interface-builder-and-i-want-to-shuffle-it) Please don't litter StackOverflow with repeats of your own questions. – Joshua Nozzi Aug 05 '10 at 13:58
-
Duplicate yes, but the first question was closed as 'not a question', so it's not like he had a choice. – Kalle Aug 05 '10 at 14:05
-
1You'll notice my name isn't in the list of closers and that I actually attempted to answer his question based on the information he provided (and a little bit of guessing). Not to mention "I opened a new question because my other was voted closed by the community" isn't really a justification. – Joshua Nozzi Aug 05 '10 at 14:14
2 Answers
1
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.
0
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
- 176,835
- 32
- 241
- 292
-
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 Aug 05 '10 at 13:56
-
1
-
1
-
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 Aug 06 '10 at 13:39
-
It's because UIButton no longer has a `setText` method. You have to do `setTitle:forState`. – zekel Nov 16 '10 at 17:36