How to specify a label to one specific storyboard?

做~自己de王妃 提交于 2019-12-11 20:14:32

问题


Basically I am making an app (a simple game) using storyboards where a question will pop up on the screen and the user has to answer it. They have the option to move onto the next question or to record their response. I have created an NSArray which has a list of questions and I want them to appear in a label randomly (the label will pick one of the questions from the NSArray at random to display). The way I want to do this though is so that each storyboard has a question so once one question has been answered you can move onto the next storyboard where the next question will be displayed in a label. The labels in each storyboard will be the same (picking questions from the same NSArray) but how do I connect a label that has been coded to a specific storyboard.

Here is the issue:

The first image shows the first storyboard where you can choose a difficulty but I don't want the label ("Question 4?") to display on this storyboard. I want it to display on storyboards such as that in the second picture

(IMAGE 1)- http://tinypic.com/r/149nuo0/8

(IMAGE 2)-http://tinypic.com/r/fk9yls/8

My question is how do I get the label to stop showing on the first image (this specific storyboard in my project)?

Here is my code:

.h file

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController
    {
        NSArray *questionArray;
        UILabel *questionLabel;
    }


@property (weak, nonatomic) IBOutlet UILabel *questionLabel;

@end

.m file

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //create question array
    questionArray = [NSArray arrayWithObjects:@"Question 1?", @"Question 2?", @"Question 3?", @"Question 4?", @"Question 5", @"Question 6", @"Question 7", @"Question 8", 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, 70)];
    [questionLabel setTextAlignment:NSTextAlignmentCenter];
    [questionLabel setText:[questionArray objectAtIndex:randomValue]];
    [self.view addSubview:questionLabel];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I am really new to programming so any help would be greatly appreciated. Thanks in advance.


回答1:


Try this.

1) Create 2 view controllers in your storyboard. Set the class of that view controller as the class name you created.

Here, I set the class for the SecondViewController.

2) Remove all the code in ViewController both .h and .m.

3) Connect properties in SecondViewController.

4) SecondViewController code:

#import "SecondViewController.h"

@interface SecondViewController ()
{
    NSArray *questionArray;
}

@end

@implementation SecondViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    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
    [self.questionLabel setTextAlignment:NSTextAlignmentCenter];
    [self.questionLabel setText:[questionArray objectAtIndex:randomValue]];
    [self.view addSubview:self.questionLabel];
}

- (IBAction)nextQuestionButtonClicked:(id)sender {

    //random a question again
    int lowerBound = 0;
    int upperBound = [questionArray count] - 1;
    int randomValue = lowerBound + arc4random() % (upperBound - lowerBound);

    [self.questionLabel setText:[questionArray objectAtIndex:randomValue]];
}

@end


来源:https://stackoverflow.com/questions/25251358/how-to-specify-a-label-to-one-specific-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!