I'm really new to programming and having a lot of trouble figuring out how to work with xcode. Im trying to connect an NSArray (which contains various questions and will display those questions in a random order) into a label I have on one of my ViewControllers in a storyboard. I've searched and tried and cant seem to get it. Can anyone help me figure it out? I feel like its really easy I'm just missing something.
Alright. Here's what you do:
1) Declare things…
@interface ViewController ()
{
NSArray *questionArray;
UILabel *questionLabel;
}
@end
2) ViewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
//create question array
questionArray = [NSArray arrayWithObjects:@"Question 1?", @"Question 2?", @"Question 3?", @"Question 4?", nil];
//random a question
int lowerBound = 0;
int upperBound = [questionArray count] - 1;
int randomValue = lowerBound + arc4random() % (upperBound - lowerBound);
//create UILabel
questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
[questionLabel setTextAlignment:NSTextAlignmentCenter];
[questionLabel setText:[questionArray objectAtIndex:randomValue]];
[self.view addSubview:questionLabel];
//create next button
nextButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 150, 320, 100)];
[nextButton setTitle:@"Next Question" forState:UIControlStateNormal];
[nextButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(nextQuestionButtonSelected) forControlEvents:UIControlEventTouchUpInside];
}
3) When button clicked..
//Create action connection in the storyboard..
- (IBAction)nextQuestionButtonClicked:(id)sender
{
//random a question again
int lowerBound = 0;
int upperBound = [questionArray count] - 1;
int randomValue = lowerBound + arc4random() % (upperBound - lowerBound);
[questionLabel setText:[questionArray objectAtIndex:randomValue]];
}