我有一个UILabel我说的是“是一天,B日等”我们的学校进行了A-G轮换,所以这告诉用户今天的哪一天。 (这些日子是从一个大的NSDictionary中提取出来的,它与字母日期和实际日期相匹配)
我想根据它的日期显示不同的图像。像
这样的东西// (in pseudo-code)
if [UILabel == @"A Day"]{
image.image = [UIImage imageNamed:@"Aday.jpg"];
}
我将A Day
放入我的标签中:[dictionary setObject:@"A Day" forKey:@"2012-05-15"];
然后执行[letterday setText:[dictionary objectForKey:LetterDay]];
。
有没有办法使用这个相同的字典,并做我上面尝试的事情?
这样做的正确方法是什么?
谢谢!
编辑:这就是我所拥有的...仍然无法正常工作。有小费吗?
NSMutableDictionary *imageforlabel = [[NSMutableDictionary alloc] init];
[imageforlabel setObject:[UIImage imageNamed:@"ADay.png"] forKey:@"A Day"];
[imageforlabel setObject:[UIImage imageNamed:@"BDay.png"] forKey:@"B Day"];
[imageforlabel setObject:[UIImage imageNamed:@"CDay.png"] forKey:@"C Day"];
[imageforlabel setObject:[UIImage imageNamed:@"DDay.png"] forKey:@"D Day"];
[imageforlabel setObject:[UIImage imageNamed:@"EDay.png"] forKey:@"E Day"];
[imageforlabel setObject:[UIImage imageNamed:@"FDay.png"] forKey:@"F Day"];
[imageforlabel setObject:[UIImage imageNamed:@"GDay.png"] forKey:@"G Day"];
rotationday.image = [imageforlabel objectForKey:letterday.TEXT];
答案 0 :(得分:5)
您正在寻找的代码是:
if ([myLabel.text isEqualToString:@"A Day"]) {
但这是编写程序的可怕方式。某处,您决定将字符串@"A Day"
放入标签中。 那是你应该决定要显示什么图像的地方。
回应您的评论:一种简单的方法是使用字典将每个日期字符串映射到适当的图像。例如:
[labelMap setObject:@"A Day" forKey:dateString];
[imageMap setObject:[UIImage imageNamed:@"Aday.jpg"] forKey:dateString];
...
letterDayLabel.text = [labelMap objectForKey:letterDay];
letterDayImage.image = [imageMap objectForKey:letterDay];
一种更复杂的方法,以及我在自己的实现中可能使用的方法,是创建一个代表轮换中特定日期的RotationDay
类,并保存每天的“资产”:当天的标签和图片。将来,如果您添加更多资源(一天中的多个图像,或短标签和长标签,动画或声音),您可以将它们添加到课程中。例如:
@interface RotationDay : NSObject
@property (strong, nonatomic, readonly) NSString *labelText;
@property (strong, nonatomic, readonly) UIImage *image;
+ (RotationDay *)dayForLetter:(NSString *)letter;
@end
@implementation RotationDay
@synthesize labelText = _labelText;
@synthesize image = _image;
static NSMutableDictionary *daysMap() {
static NSMutableDictionary *theMap;
static dispatch_once_t once;
dispatch_once(&once, ^{ theMap = [[NSMutableDictionary alloc] init]; });
return theMap;
}
+ (RotationDay *)uncachedDayForLetter:(NSString *)letter {
RotationDay *day = [[RotationDay alloc] init];
day->_labelText = [[NSString alloc] initWithFormat:@"%@ Day", letter];
NSString *imageName = [NSString stringWithFormat:@"%@day.jpg", letter];
day->_image = [UIImage imageNamed:imageName];
return day;
}
+ (RotationDay *)dayForLetter:(NSString *)letter {
NSMutableDictionary *map = daysMap();
RotationDay *day = [map objectForKey:letter];
if (!day) {
day = [self uncachedDayForLetter:letter];
[map setObject:day forKey:letter];
}
return day;
}
@end
然后,你会像这样使用它:
[dictionary setObject:[RotationDay dayForLetter:@"A"] forKey:@"2012-05-15"];
...
RotationDay *day = [dictionary objectForKey:LetterDay];
letterday.text = day.labelText;
imageday.image = day.image;
答案 1 :(得分:5)
定义一个ivar NSDictionary *imageForLabel
,并在指定的初始化程序中按如下所示进行设置:
imageForLabel = [NSDictionary dictionaryWithObjectsAndKeys:
[UIImage imageNamed:@"Aday.jpg"], @"A Day"
, [UIImage imageNamed:@"Bday.jpg"], @"B Day"
, [UIImage imageNamed:@"Cday.jpg"], @"C Day"
, // ...and so on
, nil];
然后在运行时从NSDictionary
:
UIImage *image = [imageForLabel objectForKey:label.text];
答案 2 :(得分:3)
UILabel *label;
if ([label.text isEqualToString:@"A Day"]) {
}
等
答案 3 :(得分:2)
if ([Your_LAbel.text isEqualtoString : @"A Day"])
{
//Your code;
}
答案 4 :(得分:1)
这很简单:
以下是代码:
[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[label text]]]];
在您的问题标签= @“A Day”和image =“Aday.jpg”。所以我更喜欢这个。
image = [image stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *finalImageName = [image stringByAppendingString:@"jpg"];
UIImage img = [UIImage imageNamed:finalImageName];
希望这对你有用
享受编码:)