保留随机视图之间的 UILabel 值
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使变量 Score 全局化,即在您的 AppDelegate 中定义它,您可以在整个应用程序中访问它。
Make the variable score global i.e. define this in your
AppDelegate
and you can access it throughout your application.最快但肮脏的方法是将其存储在应用程序委托中..如果您希望它在应用程序启动之间持续存在,请将其存储在用户默认值中..
但正如我所说,这很脏..
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..
由于它们是属性,因此当您创建下一个视图时,您可以将标签的文本设置为当前视图的文本,并将分数设置为当前视图的文本。
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.