保留随机视图之间的 UILabel 值

发布于 2024-12-01 15:51:31 字数 864 浏览 0 评论 0原文

我正在为 iPhone 制作一个基于视图的测验应用程序,玩家在回答问题时会在三个不同的视图之间随机切换。当回答正确时他们会得到+1,当点击错误答案时他们会得到-1。但是回答完问题后,当您进入新的随机视图时,我需要 UILabel 来显示上一个视图的分数。我该怎么做?

这是我的代码:

ViewController.h

@interface ViewController : UIViewController {

    IBOutlet UILabel *labelQuestion;
IBOutlet UILabel *labelAnswer1;
IBOutlet UILabel *labelAnswer2;
IBOutlet UILabel *labelAnswer3;
IBOutlet UILabel *labelScore;

int score;    

}

@property (nonatomic, retain) IBOutlet UILabel *labelScore;
@property (nonatomic) int score;


ViewController.m

@synthesize labelScore;
@synthesize score;

-(IBAction)CorrectAnswer; {

    score = score +1;
    labelScore.text = [NSString stringWithFormat:@"%i", score];

}

-(IBAction)WrongAnswer; {

    score = score -1;
    labelScore.text = [NSString stringWithFormat:@"%i", score];

}

I'm making a view based quiz app for the iPhone where the player is going randomly between three different views when answering the questions. They get +1 when answering correct and -1 when tapping the wrong answer. But after answering the question, when you go to the new random view, I need the UILabel to show the score from the previous view. How do I do that?

Here is my code:

ViewController.h

@interface ViewController : UIViewController {

    IBOutlet UILabel *labelQuestion;
IBOutlet UILabel *labelAnswer1;
IBOutlet UILabel *labelAnswer2;
IBOutlet UILabel *labelAnswer3;
IBOutlet UILabel *labelScore;

int score;    

}

@property (nonatomic, retain) IBOutlet UILabel *labelScore;
@property (nonatomic) int score;


ViewController.m

@synthesize labelScore;
@synthesize score;

-(IBAction)CorrectAnswer; {

    score = score +1;
    labelScore.text = [NSString stringWithFormat:@"%i", score];

}

-(IBAction)WrongAnswer; {

    score = score -1;
    labelScore.text = [NSString stringWithFormat:@"%i", score];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人│生佛魔见 2024-12-08 15:51:31

使变量 Score 全局化,即在您的 AppDelegate 中定义它,您可以在整个应用程序中访问它。

Make the variable score global i.e. define this in your AppDelegate and you can access it throughout your application.

最近可好 2024-12-08 15:51:31

最快但肮脏的方法是将其存储在应用程序委托中..如果您希望它在应用程序启动之间持续存在,请将其存储在用户默认值中..
但正如我所说,这很脏..

the quickest but dirty way is to store it in app delegate.. and if you want it to persist between app launches, store it in user defaults..
but as i said this is dirty..

等你爱我 2024-12-08 15:51:31

由于它们是属性,因此当您创建下一个视图时,您可以将标签的文本设置为当前视图的文本,并将分数设置为当前视图的文本。

As they're properties, when you create the next view you can set the label's text to the current view's text and the score to the current view's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文