2013-12-03 54 views
-1

我正在开发应用程序,并试图根据星期几实现不同的文本标签消息。 ex。如果如何动态更改UIText字段

 
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" 

我在故事板中创建了一个标签,并希望它根据应用打开的日子更改消息。

感谢您的帮助!

+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 [email protected]"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]; 
    } 
} 

使用此代码这会给你根据当前日

+0

谢谢soooo多!惊人! – user3062853

0

您必须使用一个字符串数组,其中包含7个项目(索引编号从0到6),并根据当前工作日将其中一个字符串分配给您的标签的text属性。