2013-12-03 81 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屬性。