Ios Objective-C更改UILabel文本
我做了一个问答游戏。我在屏幕上放了一个问题标签。当我试图改变它的文本时,文本没有改变 QuizScreen.hIos Objective-C更改UILabel文本,ios,objective-c,Ios,Objective C,我做了一个问答游戏。我在屏幕上放了一个问题标签。当我试图改变它的文本时,文本没有改变 QuizScreen.h #import <UIKit/UIKit.h> int score = 0; int lives = 3; int QuestionSelected = 0; NSString *AnswerRunning; @interface QuizScreen : UIViewController { IBOutlet UITextField *Answer;
#import <UIKit/UIKit.h>
int score = 0;
int lives = 3;
int QuestionSelected = 0;
NSString *AnswerRunning;
@interface QuizScreen : UIViewController
{
IBOutlet UITextField *Answer;
IBOutlet UIButton *Go;
IBOutlet UILabel *Question;
IBOutlet UILabel *Session;
IBOutlet UILabel *Lives;
IBOutlet UILabel *Score;
IBOutlet UIButton *Exit;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(IBAction)GoBtn:(id)sender;
-(void)questioning;
@end
当我运行它并编写答案时,标签的文本没有变化。我尝试了stringWithFormat,但它不起作用。奇怪的是会话标签的文本发生了变化。但我找不到任何差异似乎问题标签的文本只在方法中发生了变化: -(b)质询 但我找不到你在哪里打电话。在它的入口设置断点,你们永远也到不了那个里。据我所知,应该在viewDidLoad和GoBtn:methods末尾调用您的代码[自问] 附言。
你应该注意,和。特别是命名变量、数据类型、方法(例如变量不应以大写字母开头,类或结构应以大写字母开头)您是否调试并检查所有设置是否正确?标签是否只有默认文本?标签中打印的内容所有标签都不起作用,或者任何用于检查标签中存储的值的实现NSLog都不起作用。请检查您的iboutlet连接是否正确连接我是Objective-C iOS SDK的新手(我刚刚在youtube上观看了一个视频)。我会注意它。
#import "QuizScreen.h"
@interface QuizScreen ()
@end
@implementation QuizScreen
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
-(IBAction)GoBtn:(id)sender{
switch (QuestionSelected) {
case 0:
if ([Answer.text isEqualToString: @"YOUR WORD"]){
score = score + 1;
Score.text = [NSString stringWithFormat:@"Score: %i" , score];
Session.text = [NSString stringWithFormat:@"Correct Answer!! Score +1"];
}else{
lives = lives - 1;
Lives.text = [NSString stringWithFormat:@"Lives: %i" , lives];
Session.text = [NSString stringWithFormat:@"Wrong Answer!!! Lives -1"];
}
QuestionSelected = QuestionSelected + 1;
break;
case 1:
if ([Answer.text isEqualToString: @"YOUR WORD"]){
score = score + 1;
Score.text = [NSString stringWithFormat:@"Score: %i" , score];
Session.text = [NSString stringWithFormat:@"Correct Answer!! Score +1"];
}else{
lives = lives - 1;
Lives.text = [NSString stringWithFormat:@"Lives: %i" , lives];
Session.text = [NSString stringWithFormat:@"Wrong Answer!!! Lives -1"];
}
QuestionSelected = QuestionSelected + 1;
break;
case 2:
if ([Answer.text isEqualToString: @"YOUR WORD"]){
score = score + 1;
Score.text = [NSString stringWithFormat:@"Score: %i" , score];
Session.text = [NSString stringWithFormat:@"Correct Answer!! Score +1"];
}else{
lives = lives - 1;
Lives.text = [NSString stringWithFormat:@"Lives: %i" , lives];
Session.text = [NSString stringWithFormat:@"Wrong Answer!!! Lives -1"];
}
QuestionSelected = QuestionSelected + 1;
break;
case 3:
if ([Answer.text isEqualToString: @"YOUR WORD"]){
score = score + 1;
Score.text = [NSString stringWithFormat:@"Score: %i" , score];
Session.text = [NSString stringWithFormat:@"Correct Answer!! Score +1"];
}else{
lives = lives - 1;
Lives.text = [NSString stringWithFormat:@"Lives: %i" , lives];
Session.text = [NSString stringWithFormat:@"Wrong Answer!!! Lives -1"];
}
QuestionSelected = QuestionSelected + 1;
break;
case 4:
if ([Answer.text isEqualToString: @"YOUR WORD"]){
score = score + 1;
Score.text = [NSString stringWithFormat:@"Score: %i" , score];
Session.text = [NSString stringWithFormat:@"Correct Answer!! Score +1"];
}else{
lives = lives - 1;
Lives.text = [NSString stringWithFormat:@"Lives: %i" , lives];
Session.text = [NSString stringWithFormat:@"Wrong Answer!!! Lives -1"];
}
QuestionSelected = QuestionSelected + 1;
break;
case 5:
if ([Answer.text isEqualToString: @"YOUR WORD"]){
score = score + 1;
Score.text = [NSString stringWithFormat:@"Score: %i" , score];
Session.text = [NSString stringWithFormat:@"Correct Answer!! Score +1"];
}else{
lives = lives - 1;
Lives.text = [NSString stringWithFormat:@"Lives: %i" , lives];
Session.text = [NSString stringWithFormat:@"Wrong Answer!!! Lives -1"];
}
QuestionSelected = QuestionSelected + 1;
break;
case 6:
if ([Answer.text isEqualToString: @"YOUR WORD"]){
score = score + 1;
Score.text = [NSString stringWithFormat:@"Score: %i" , score];
Session.text = [NSString stringWithFormat:@"Correct Answer!! Score +1"];
}else{
lives = lives - 1;
Lives.text = [NSString stringWithFormat:@"Lives: %i" , lives];
Session.text = [NSString stringWithFormat:@"Wrong Answer!!! Lives -1"];
}
QuestionSelected = QuestionSelected + 1;
break;
case 7:
if ([Answer.text isEqualToString: @"YOUR WORD"]){
score = score + 1;
Score.text = [NSString stringWithFormat:@"Score: %i" , score];
Session.text = [NSString stringWithFormat:@"Correct Answer!! Score +1"];
}else{
lives = lives - 1;
Lives.text = [NSString stringWithFormat:@"Lives: %i" , lives];
Session.text = [NSString stringWithFormat:@"Wrong Answer!!! Lives -1"];
}
break;
default:
break;
}
}
-(void)questioning {
switch (QuestionSelected) {
case 0:
Question.text = @"You can only keep it once you give it to somebody.What is it?";
break;
case 1:
Question.text = @"Light hides me and darkness kills me.What am I?";
break;
case 2:
Question.text = @"Voiceless it cries,Wingless flutters,Toothless bites,Mouthless mutters.";
break;
case 3:
Question.text = @"What goes in the water black and comes out red?";
break;
case 4:
Question.text = @"It's hard to give up.Remove part of it and you still have a bit.Remove another part, but bit is still there.Remove another and it remains.What is it?";
break;
case 5:
Question.text = @"With pointed fangs I sit and wait,with piercing force I serve out fate.Grabbing bloodless victims, proclaiming my might;physically joining with a single bite.What am I?";
break;
case 6:
Question.text = @"Jasmine has a toaster with two slots that toasts one side of each piece of bread at a time, and it takes one minute to do so.If she wants to make 3 pieces of toast, what is the least amount of time she needs to toast them on both sides?";
break;
case 7:
Question.text = @"A man was born on January 1st, 23 B.C. and died January 2nd, 23 A.D. How old did he live to be?";
break;
default:
break;
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end