Objective c 如何为一个特定的情节提要指定标签?
基本上,我是在用故事板制作一个应用程序(一个简单的游戏),屏幕上会弹出一个问题,用户必须回答。他们可以选择进入下一个问题或记录他们的回答。我已经创建了一个NSArray,其中有一个问题列表,我希望它们随机出现在标签中(标签将随机从NSArray中选择一个问题来显示)。我想这样做的方式是,每个故事板都有一个问题,因此,一旦回答了一个问题,您就可以转到下一个故事板,其中下一个问题将显示在标签中。每个故事板中的标签都是相同的(从同一个NSArray中挑选问题),但是如何将已编码的标签连接到特定的故事板 问题是: 第一幅图像显示了第一个故事板,您可以在其中选择难度,但我不希望标签(“问题4?”)显示在此故事板上。我希望它显示在故事板上,如第二张图片中的那样 (图1)- (图2)- 我的问题是如何让标签停止显示在第一张图片上(我的项目中的这个特定故事板) 这是我的密码: .h文件Objective c 如何为一个特定的情节提要指定标签?,objective-c,xcode,storyboard,nsarray,label,Objective C,Xcode,Storyboard,Nsarray,Label,基本上,我是在用故事板制作一个应用程序(一个简单的游戏),屏幕上会弹出一个问题,用户必须回答。他们可以选择进入下一个问题或记录他们的回答。我已经创建了一个NSArray,其中有一个问题列表,我希望它们随机出现在标签中(标签将随机从NSArray中选择一个问题来显示)。我想这样做的方式是,每个故事板都有一个问题,因此,一旦回答了一个问题,您就可以转到下一个故事板,其中下一个问题将显示在标签中。每个故事板中的标签都是相同的(从同一个NSArray中挑选问题),但是如何将已编码的标签连接到特定的故事板
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSArray *questionArray;
UILabel *questionLabel;
}
@property (weak, nonatomic) IBOutlet UILabel *questionLabel;
@end
#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
我真的是一个新的编程,所以任何帮助将不胜感激。提前谢谢。试试这个
1) 在情节提要中创建2个视图控制器。将该视图控制器的类设置为您创建的类名
在这里,我为SecondViewController设置了类
2) 删除ViewController
中的所有代码,包括.h
和.m
3) 在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
你的代码在上面。它是用于第一视图控制器(图1)还是第二视图控制器(图2)?我相信它是用于我的第一视图控制器。但我只有一个viewcontroller.h和.m文件。我刚才在main.storyboard中拖放的其他视图控制器。我不知道如何访问故事板中每个特定viewcontroller的代码。如果你不明白我的意思,我可以发一张我的xcode项目的照片,所以你想说的是。你想在第二个视图控制器(图2)中回答这个问题吗?是的,我不想在第一个视图控制器(图1)中回答这个问题。如何解决图1中的问题?