views:

98

answers:

2

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
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
Just loop through all of the buttons then and swap at random
iWasRobbed
Who would want to use an application with 100 shuffling buttons?!
dreamlax
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
+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 the origin member of CGRect, 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 of UIButton.
  • 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 but rand() should suffice for your needs. Just ensure to call srand() with some seed before your first call to rand().
  • 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