How to shuffle button or title over button?
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 p开发者_开发问答ossible.Its an Iphone app.
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.
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.
精彩评论