我试图以图片形式提出一个问题并且它无法正常工作,我知道如果问题出现在文本中则会是这样的
QusetionText.text=[NSString stringWithFormat@:"Who won World cup in 2006?"];
但我怎么用图片形式呢?
以下是代码:
-(void) ACat{
switch (QuestionSelected) {
case 0:
image = [UIImage imageNamed:@"question1.jpg"];
[UIImageView setImage: image]; //ERROR HERE
[Answer1 setTitle:@"GTA" forState:UIControlStateNormal];
[Answer2 setTitle:@"Minecraft" forState:UIControlStateNormal];
[Answer3 setTitle:@"COD" forState:UIControlStateNormal];
[Answer4 setTitle:@"VatchDog" forState:UIControlStateNormal];
Answer1Correct = YES;
break;
case 1:
UIImage *image = [UIImage imageNamed:@"question2.png"];
UIImageView.image = image; //ERROR HERE
[Answer1 setTitle:@"LOL" forState:UIControlStateNormal];
[Answer2 setTitle:@"Minecraft" forState:UIControlStateNormal];
[Answer3 setTitle:@"DOTA" forState:UIControlStateNormal];
[Answer4 setTitle:@"FIFA" forState:UIControlStateNormal];
Answer2Correct = YES;
break;
case 2:
UIImage *image = [UIImage imageNamed:@"question3.jpg"];
UIImageView.image = image; //ERROR HERE
[Answer1 setTitle:@"LOL" forState:UIControlStateNormal];
[Answer2 setTitle:@"Minecraft" forState:UIControlStateNormal];
[Answer3 setTitle:@"DOTA" forState:UIControlStateNormal];
[Answer4 setTitle:@"FIFA" forState:UIControlStateNormal];
Answer4Correct = YES;
break;
case 3:
UIImage *image = [UIImage imageNamed:@"question4.jpeg"];
UIImageView.image = image; //ERROR HERE
[Answer1 setTitle:@"LOL" forState:UIControlStateNormal];
[Answer2 setTitle:@"Minecraft" forState:UIControlStateNormal];
[Answer3 setTitle:@"DOTA" forState:UIControlStateNormal];
[Answer4 setTitle:@"Vatchdog" forState:UIControlStateNormal];
Answer4Correct = YES;
break;
default:
break;
}
}
.h文件
#import <UIKit/UIKit.h>
int CategoryLoaded;
int QuestionSelected;
BOOL Answer1Correct;
BOOL Answer2Correct;
BOOL Answer3Correct;
BOOL Answer4Correct;
@interface contentViewController : UIViewController
{
IBOutlet UILabel *lcat;
IBOutlet UIImageView *image;
IBOutlet UIButton *Answer1;
IBOutlet UIButton *Answer2;
IBOutlet UIButton *Answer3;
IBOutlet UIButton *Answer4;
}
-IBAction Answer1:(id)sender;
-IBAction Answer2:(id)sender;
-IBAction Answer3:(id)sender;
-IBAction Answer4:(id)sender;
-(void) ACat;
-(void) Rpg;
@end
答案 0 :(得分:0)
这应该有效:
-(void) ACat{
switch (QuestionSelected) {
case 0:
image.image = [UIImage imageNamed:@"question1.jpg"];
[Answer1 setTitle:@"GTA" forState:UIControlStateNormal];
[Answer2 setTitle:@"Minecraft" forState:UIControlStateNormal];
[Answer3 setTitle:@"COD" forState:UIControlStateNormal];
[Answer4 setTitle:@"VatchDog" forState:UIControlStateNormal];
Answer1Correct = YES;
break;
case 1:
image.image = [UIImage imageNamed:@"question2.png"];
[Answer1 setTitle:@"LOL" forState:UIControlStateNormal];
[Answer2 setTitle:@"Minecraft" forState:UIControlStateNormal];
[Answer3 setTitle:@"DOTA" forState:UIControlStateNormal];
[Answer4 setTitle:@"FIFA" forState:UIControlStateNormal];
Answer2Correct = YES;
break;
case 2:
image.image = [UIImage imageNamed:@"question3.jpg"];
[Answer1 setTitle:@"LOL" forState:UIControlStateNormal];
[Answer2 setTitle:@"Minecraft" forState:UIControlStateNormal];
[Answer3 setTitle:@"DOTA" forState:UIControlStateNormal];
[Answer4 setTitle:@"FIFA" forState:UIControlStateNormal];
Answer3Correct = YES;
break;
case 3:
image.image = [UIImage imageNamed:@"question4.jpeg"];
[Answer1 setTitle:@"LOL" forState:UIControlStateNormal];
[Answer2 setTitle:@"Minecraft" forState:UIControlStateNormal];
[Answer3 setTitle:@"DOTA" forState:UIControlStateNormal];
[Answer4 setTitle:@"Vatchdog" forState:UIControlStateNormal];
Answer4Correct = YES;
break;
}
}
您试图将UIImage
分配给类型为image
的{{1}}实例变量,然后尝试在UIImageView
类本身上调用setImage:
,而不是类的实例。