0

I am new to Objective-C and X-Code and basically programming in iOS so this may be a very simple question. Anyway, I'm trying to make a logo quiz wherein the logos and the choices are generated in random order i.e. (Logo 1) --- Choices: A B C D for the first try. Then the next time the user opens the app, Logo 4 appears first with choices A D B C. (I hope that's clear). I've managed to randomly display the logos but can't figure out how to do the same for the choices which would modify the value of 4 buttons below the logo.

From what I've search, you use something like:

 [btnA setTitle:@"answer" forState:UIControlStateNormal];

My problem is, I'm thinking of putting the choices into an array as well so I tried something such as this:

[btnA setTitle:[answerArray objectAtIndex:i] forState:UIControlStateNormal];]

Similar to what I've done for the logos but I'm not getting any values for the button. Is there something I'm missing here? I don't get any errors either. Here's a part of my code that might better explain what I've been trying to do:

NSMutableArray *answerList = [[NSMutableArray alloc] initWithObjects:
                               @"AnswerA",
                               @"AnswerB",
                               @"AnswerC",
                               nil];
answerArray = answerList;
int i = arc4random() % [answerArray count];
[self.btn1 setTitle:[answerArray objectAtIndex:i] forState:UIControlStateNormal];
[self.btn2 setTitle:[answerArray objectAtIndex:i] forState:UIControlStateNormal];
[self.btn3 setTitle:[answerArray objectAtIndex:i] forState:UIControlStateNormal];
user1597438
  • 2,171
  • 5
  • 35
  • 78
  • 1
    answerArray must contain objects of kind NSString or NSMutableString... if this is ok, then kindly post more code – Anoop Vaidya Jan 18 '13 at 09:33
  • Thanks for that suggestion. I think that's one of the points where I went wrong, I'll update my post to show more code. – user1597438 Jan 18 '13 at 09:36
  • when did you init your buttons? from a nib? self.btn1 might be nil when you set the titles – Di Wu Jan 18 '13 at 09:42
  • I didn't init the buttons since I didn't there was a need for it. And sorry, I'm not exactly sure what a nib is. Just started coding in Objective-C yesterday. – user1597438 Jan 18 '13 at 09:51
  • nib is historical name for xib.... and i assume you are familiar with xib.. – Anoop Vaidya Jan 18 '13 at 09:59

2 Answers2

1

Just Shuffle your buttonTitleArray which is MutableArray without repeating any same logo quiz using this link.

Also Shuffle logo quiz's choice to make it interactive with above link.

Community
  • 1
  • 1
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • I don't understand the idea of shuffling mutable arrays yet but I'll try this one once I understand how it works. Thanks for the answer. – user1597438 Jan 18 '13 at 09:52
1

See following code - it can generate random title from set of 100:

-(int)getRandomNumber:(int)fromVal to:(int)toVal {
    return (int)fromVal + arc4random() % (toVal-fromVal+1);
}

-(void) setButtonTitle
{
    NSArray * buttonTitleArray = [NSArray arrayWithObjects:@"title1", @"title2",... @"title100", nil};
    int x = [getRandomNumber :0 :100];
    NSString * title = [buttonTitleArray objectAtIndex:x];
    [button setTitle:title :forState:UIControlStateNormal];
}
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • what if random number from 0 to 100 is greater than buttonTitleArray's count? – Paresh Navadiya Jan 18 '13 at 10:00
  • change argument values to getRandomNumber. – Nirav Bhatt Jan 18 '13 at 10:33
  • I tried this solution and I got an error around int x = [getRandomNumber :0 :100];. It said that getRandomNumber is undeclared. I'm pretty sure I added -(int)getRandomNumber:(int)fromVal to:(int)toVal in the .h file so I'm not exactly sure what I'm missing here. – user1597438 Jan 21 '13 at 01:08
  • Actually, never mind. I just figured it out, I had an image over the button so obviously the texts won't appear. I just changed into into background. (I feel like shooting myself for that.) Thank you again – user1597438 Jan 21 '13 at 02:33