我正在开发一款应用,并试图根据星期几来获得不同的文字标签信息。 恩。如果
Monday: "Today's Hours are: 8:00am - 6:00pm" Tuesday: "Today's Hours are: 8:00am - 6:00pm" Wednesday: "Today's Hours are: 8:00am - 6:00pm" Thursday: "Today's Hours are: 8:00am - 6:00pm" Friday: "Today's Hours are: 8:00am - 6:00pm" Saturday: "Today's Hours are: 9:00am - 2:00pm" Sunday: "Sorry we are closed today"
我在故事板中创建了一个标签,并希望它根据应用程序打开的日期更改消息。
感谢您的帮助!
答案 0 :(得分:0)
你必须使用一个字符串数组,其中包含7个项目(索引从0到6),并根据当前工作日将其中一个字符串分配给标签的text
属性。
答案 1 :(得分:0)
您可以在viewDidLoad方法中使用此代码
NSDate* currentDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* nowTimeZone = [NSTimeZone systemTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:currentDate];
NSInteger nowGMTOffset = [nowTimeZone secondsFromGMTForDate:currentDate];
NSTimeInterval interval = nowGMTOffset - currentGMTOffset;
NSDate* nowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:nowDate];
NSString *timing = @"8:00am - 6:00pm";
NSString *description =@"Today's Hours are";
NSArray *days = @[@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",@"Sunday",];
for (NSString *str in days) {
if ([str isEqualToString:@"Saturday"]) {
timing = @"9:00am - 2:00pm";
}
if ([str isEqualToString:@"Sunday"]) {
description = @"Sorry we are closed today";
timing = nil;
}
if ([str isEqualToString:dayName]) {
NSLog(@"%@ : %@ :%@",str,description,timing);
//here you can write yourLabelname.text =[NSString stringWithFormat:@"%@ : %@ :%@",str,description,timing];
}
}
这将根据当前日期给你结果