我正在开发iOS应用程序时出现问题:我在两个屏幕中使用了switch语句来处理多个问题的回复,但它们似乎无法正常工作:也许我犯了一些错误,但是由于我还在学习iOS,我不确定它是什么。
以下是这些屏幕的代码:
Riddle.m
#import "Riddle.h"
@interface Riddle()
@end
@implementation Riddle
int reply;
-(void) viewDidLoad
{
_etiqueta = @" 1: Que es allò blanc que tenia Santiago?";
self.label.text = self.etiqueta;
_eti = @"";
self.lab.text = self.eti;
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard {
[_resposta resignFirstResponder];
}
-(IBAction) buttonPressed: (id) sender
{
switch (reply)
{
case 1:
{
_resposta.text = @"cavall";
_eti = @"Correcte!";
_resposta.text = @"";
self.lab.text = self.eti;
_etiqueta = @" 2: Què portes als peus?";
self.label.text = self.etiqueta;
break;
}
case 2:
{
_resposta.text = @"sabates";
_eti = @"Correcte!";
_resposta.text = @"";
self.lab.text = self.eti;
_etiqueta = @" 3: Què tires en l'oca?";
break;
}
case 3:
{
_resposta.text = @"daus";
_eti = @"Correcte!";
_resposta.text = @"";
self.lab.text = self.eti;
_etiqueta = @" 4: Amb què ve l'hamburguesa?";
break;
}
case 4:
{
_resposta.text = @"patates";
_eti = @"Correcte!";
_resposta.text = @"";
self.lab.text = self.eti;
_etiqueta = @" 5: Què té el pegàs?";
self.label.text = self.etiqueta;
break;
}
case 5:
{
_resposta.text = @"ales";
_eti = @"Correcte! Les has encertades totes!";
self.lab.text = self.eti;
break;
}
default:
{
_eti = @"Incorrecte!";
_resposta.text = @"";
self.lab.text = self.eti;
break;
}
}
}
@end
Riddle.h
@interface Riddle : UIViewController
@property (weak) IBOutlet UILabel * label;
@property (weak) IBOutlet UITextField * resposta;
@property (weak) IBOutlet UIButton * enviar;
@property (weak) IBOutlet UILabel * lab;
@property (strong, atomic) NSString * etiqueta;
@property (strong, atomic) NSString * eti;
@end
Colors.m
#import "Colors.h"
@interface Colors()
@end
@implementation Colors
-(void) viewdidLoad{
[super viewDidLoad];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10, 240, 300, 30)];
[self.view addSubview: label];
label.text = @"Introdueix el color que vols que es mostri.";
label.numberOfLines = 4;
label.textColor = [UIColor blackColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard {
[_color resignFirstResponder];
}
int colored;
-(IBAction) buttonPressed: (id) sender
{
switch(colored)
{
case 1:{
_color.text = @"vermell";
_color.text=@"";
_correcte=@"Color vàlid";
self.correct.text = self.correcte;
UIView *un = [[UIView alloc] initWithFrame: CGRectMake(50, 50, 50, 50)];
self.un.backgroundColor =[UIColor colorWithPatternImage: [UIImage imageNamed:@"Red.jpg"]];
break;
}
case 2:{
_color.text = @"blau";
_color.text=@"";
_correcte=@"Color vàlid";
self.correct.text = self.correcte;
UIView *dos = [[UIView alloc] initWithFrame: CGRectMake(50, 50, 50, 50)];
self.dos.backgroundColor =[UIColor colorWithPatternImage: [UIImage imageNamed:@"blue.jpg"]];
break;
}
case 3:{
_color.text = @"groc";
_color.text=@"";
_correcte=@"Color vàlid";
self.correct.text = self.correcte;
UIView *tres = [[UIView alloc] initWithFrame: CGRectMake(50, 50, 50, 50)];
self.tres.backgroundColor =[UIColor colorWithPatternImage: [UIImage imageNamed:@"yellow.jpg"]];
break;
}
case 4:{
_color.text = @"marro";
_color.text=@"";
_correcte=@"Color vàlid";
self.correct.text = self.correcte;
UIView *quatre = [[UIView alloc] initWithFrame: CGRectMake(50, 50, 50, 50)];
self.quatre.backgroundColor =[UIColor colorWithPatternImage: [UIImage imageNamed:@"brown.jpg"]];
break;
}
case 5:
{
_color.text = @"rosa";
_color.text=@"";
_correcte=@"Color vàlid";
self.correct.text = self.correcte;
UIView *cinc = [[UIView alloc] initWithFrame: CGRectMake(50, 50, 50, 50)];
self.cinc.backgroundColor =[UIColor colorWithPatternImage: [UIImage imageNamed:@"pink.jpg"]];
break;
}
case 6:
{
_color.text = @"taronja";
_color.text=@"";
_correcte=@"Color vàlid";
self.correct.text = self.correcte;
UIView *sis = [[UIView alloc] initWithFrame: CGRectMake(50, 50, 50, 50)];
self.sis.backgroundColor =[UIColor colorWithPatternImage: [UIImage imageNamed:@"orange.jpg"]];
break;
}
case 7:
{
_color.text = @"negre";
_color.text=@"";
_correcte=@"Color vàlid";
self.correct.text = self.correcte;
UIView *set = [[UIView alloc] initWithFrame: CGRectMake(50, 50, 50, 50)];
self.set.backgroundColor =[UIColor colorWithPatternImage: [UIImage imageNamed:@"black.jpg"]];
break;
}
case 8:
{
_color.text =@"verd";
_color.text=@"";
_correcte=@"Color vàlid";
self.correct.text = self.correcte;
UIView *vuit = [[UIView alloc] initWithFrame: CGRectMake(50, 50, 50, 50)];
self.vuit.backgroundColor =[UIColor colorWithPatternImage: [UIImage imageNamed:@"green.jpg"]];
break;
}
default:
{
_color.text = @"blanc";
[[[UIAlertView alloc] initWithTitle:@"Alerta!"
message:@"Color no vàlid!"
delegate: nil
cancelButtonTitle:@"Cancel·lar"
otherButtonTitles:nil
] show];
_color.text=@"";
break;
}
}
}
@end
Colors.h
@interface Colors : UIViewController
@property (weak) IBOutlet UILabel * label;
@property (weak) IBOutlet UITextField * color;
@property (weak) IBOutlet UIButton * enviar;
@property (weak) IBOutlet UILabel * correct;
@property (strong, nonatomic) NSString * correcte;
@property (weak) IBOutlet UIView * un;
@property (weak) IBOutlet UIView * dos;
@property (weak) IBOutlet UIView * tres;
@property (weak) IBOutlet UIView * quatre;
@property (weak) IBOutlet UIView * cinc;
@property (weak) IBOutlet UIView * sis;
@property (weak) IBOutlet UIView * set;
@property (weak) IBOutlet UIView * vuit;
@end
如果你能帮助我解决这些问题,我将非常感激。