Fit*_*itz -2 iphone xcode objective-c ios ios5
我有一个UILabel我说的是"是一天,B天等" 我们的学校正在进行AG轮换,所以这告诉用户今天的哪一天.(这些日子是从一个大的NSDictionary中提取出来的,它与实际日期相匹配)
我想根据它的日期显示不同的图像.所以像
// (in pseudo-code)
if [UILabel == @"A Day"]{
image.image = [UIImage imageNamed:@"Aday.jpg"];
}
Run Code Online (Sandbox Code Playgroud)
我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];
Run Code Online (Sandbox Code Playgroud)
您正在寻找的代码是:
if ([myLabel.text isEqualToString:@"A Day"]) {
Run Code Online (Sandbox Code Playgroud)
但这是编写程序的一种可怕方式.在某个地方,你决定将字符串@"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];
Run Code Online (Sandbox Code Playgroud)
一个稍微比较复杂的方法,什么我可能会在自己的实现中使用,是建立一个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
Run Code Online (Sandbox Code Playgroud)
然后,你会像这样使用它:
[dictionary setObject:[RotationDay dayForLetter:@"A"] forKey:@"2012-05-15"];
...
RotationDay *day = [dictionary objectForKey:LetterDay];
letterday.text = day.labelText;
imageday.image = day.image;
Run Code Online (Sandbox Code Playgroud)
定义一个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];
Run Code Online (Sandbox Code Playgroud)
然后在运行时查找以下内容NSDictionary:
UIImage *image = [imageForLabel objectForKey:label.text];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3734 次 |
| 最近记录: |