Instead of generating a random number and checking if that number has already been used, I would create an NSMutableArray of the numbers 0 to 29 (each wrapped in an NSNumber) and then randomly shuffle the array using the category supplied by Gregory Goltsov in this SO question whats-the-best-way-to-shuffle-an-nsmutablearray.
Then you just iterate over each NSNumber object from the start of the NSMutable array. i.e.
#import "NSMutableArray_Shuffling.h // From Gregory Goltsov
NSMutableArray* randomNumbers = [[NSMutableArray alloc] init];
for(int i=0; i<30; i++) {
[randomNumbers addObject:[NSNumber numberWithInt:i]];
}
[randomNumbers shuffle]; // From Gregory Goltsov
...
int lastIndex = 0;
-(IBAction)randomear
{
if (lastIndex<30) {
NSNumber* theRandomNumber = [randomNumbers objectAtIndex:lastIndex];
int theQuestion = [theRandomNumber intValue];
if (theQuestion == 0) {
labelpregunta.text = @"Las ovejas pueden nadar?";
}
if (theQuestion == 1) {
labelpregunta.text = @"Con que se tiñe la lana de negro?";
}
if (theQuestion == 2) {
labelpregunta.text = @"De que material es el mejor casco?";
}
if (theQuestion == 3){
labelpregunta.text = @"Para fabricar lana necesitas 4 _____";
}
//et cetera/ etcétera
lastIndex++;
} else {
// No more questions
}
}
However, it may be better to fill the array with a series of objects that contains both the question and the answer for a single question. i.e.
@interface aQuestion : NSObject
@property (nonatomic, string) NSString* question;
@property (nonatomic, string) NSString* answer;
-(void)initWithQuestion:(NSString)aQuestion and:(NSString) anAnswer;
-(BOOL)isCorrectAnswer(NSString testAnswer);
@end
@implementation aQuestion
-(void)initWithQuestion:(NSString*)aQuestion and:(NSString*) anAnswer
{
if(!(self=[super init])) return self;
question = aQuestion;
answer = anAnswer;
return self;
}
-(BOOL)isCorrectAnswer(NSString testAnswer)
{
[answer isEqualToString:testAnswer];
}
@end
...
#import "NSMutableArray_Shuffling.h // From Gregory Goltsov
NSMutableArray* questions = [[NSMutableArray alloc] init];
[questions addObject:[[aQuestion alloc] initWithQuestion:@"Question 1" and:@"Answer 1"]];
[questions addObject:[[aQuestion alloc] initWithQuestion:@"Question 2" and:@"Answer 2"]];
[questions addObject:[[aQuestion alloc] initWithQuestion:@"Question 3" and:@"Answer 3"]];
[questions addObject:[[aQuestion alloc] initWithQuestion:@"Question 4" and:@"Answer 4"]];
[questions shuffle]; // From Gregory Goltsov
...
for(int i=0; i<[questions count]; i++) {
aQuestion* theQuestion = [questions objectAtIndex:i];
// Ask theQuestion.question
labelpregunta.text = theQuestion.question;
...
// wait for theAnswer
....
NSString theAnswer = labelrespuesta.text;
if ([theQuestion isCorrectAnswer:theAnswer]) {
// You win!!!!!!!
}
}
// No more questions
Edit
I initially said Kristopher Johnson's answer, but I really meant Gregory Goltsov's answer
(Y mi español es peor que el Inglés)