2013-12-03 4 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

Спасибо, что так много! Удивительно! – user3062853

0

Вы должны использовать массив строк с 7 элементами в нем (с индексом от 0 до 6) и назначить одну из своих строк свойству text вашего лейбла, основанному на текущем буднем дне.

Смежные вопросы