如何为一个特定的情节提要板指定标签? [英] How to specify a label to one specific storyboard?
问题描述
基本上,我使用情节提要制作一个应用程序(一个简单的游戏),其中的问题会在屏幕上弹出,用户必须回答.他们可以选择移至下一个问题或记录其回答.我创建了一个包含问题列表的NSArray,并且希望它们随机出现在标签中(标签将从NSArray中随机选择一个问题进行显示).不过,我要这样做的方法是使每个情节提要板都有一个问题,因此,一旦回答了一个问题,您就可以进入下一个情节提要板,在该情节提要中下一个问题将显示在标签中.每个情节提要中的标签都是相同的(从相同的NSArray中选择问题),但是如何将已编码的标签连接到特定的情节提要.
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.
问题出在这里:
第一张图显示了第一个情节提要,您可以在其中选择难度,但是我不希望标签(问题4?")显示在此情节提要上.我希望它可以显示在故事板上,例如第二张图片中
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
(图像1)- http://tinypic.com/r/149nuo0/8
(图像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)?
这是我的代码:
.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSArray *questionArray;
UILabel *questionLabel;
}
@property (weak, nonatomic) IBOutlet UILabel *questionLabel;
@end
.m文件
#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)在情节提要中创建2个视图控制器.将该视图控制器的类设置为您创建的类名称.
1) Create 2 view controllers in your storyboard. Set the class of that view controller as the class name you created.
在这里,我为SecondViewController设置了类.
Here, I set the class for the SecondViewController.
2)删除ViewController
和.m
中ViewController
中的所有代码.
2) Remove all the code in ViewController
both .h
and .m
.
3)连接SecondViewController
中的属性.
4)SecondViewController
代码:
#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
这篇关于如何为一个特定的情节提要板指定标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!