In my application I'm having 4 buttons. On each click I want to show the UIButton
title shuffled value from array.
I tried this one What's the Best Way to Shuffle an NSMutableArray?
But its not working.
Asked
Active
Viewed 143 times
0
-
Can you post your code what you have tried, then you might get helped. – iPhone Programmatically Oct 19 '12 at 12:31
2 Answers
1
You will get your index of array shuffled and then use that index to pick value.
int rndIndex = arc4random()%[yourArray count];
[yourArray objectAtIndex:rndIndex];

iPhone Programmatically
- 1,211
- 2
- 22
- 54
0
Someplace you make an array :
In your header (.h) file
NSArray *words;
and a little below that
- (IBAction)aButtonWasClicked:(id)sender;
In you code file (.m), like viewDidLoad
words = [[NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil] retain];
(in ios 6 you might do it like this
words = @[@"One", @"Two", @"Three", @"Four"];
Then you do this :
- (IBAction)aButtonWasClicked:(id)sender {
int value = arv4random() % ([words count] -1);
UIButton *mybutton = (UIButton *)sender;
[mybbutton setTitle:[words objectAtIndex:value] forState:UIControlStateNormal];
}
Just remember to hook all 4 buttons to the same Action.
Thats all there is to it. You can easily add a function to make sure no button has the same title and so on.

Trausti Thor
- 3,722
- 31
- 41
-
-
My bad. I have updated the answer. This happens when you write code from memory only :) It is setTitle with forState. Answer updated – Trausti Thor Oct 19 '12 at 13:00