我希望每天在给定时间内更改值TextField

时间:2016-12-29 21:13:47

标签: objective-c

我希望每天在给定时间内更改TextField值,并且该值全天保留。

- (void)viewDidLoad {
        [super viewDidLoad];


    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    [componentsForReferenceDate setDay:27];
    [componentsForReferenceDate setMonth:10];
    [componentsForReferenceDate setYear:2016];

    NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];

    // set components for time 7:00 a.m.

    NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];

    [componentsForFireDate setHour:23];
    [componentsForFireDate setMinute:59];
    [componentsForFireDate setSecond:0];

    _utr.text = @"01:57";

1 个答案:

答案 0 :(得分:0)

假设您要在13:00在Label上设置文本,然后按如下方式保存此时间 -

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *KEY_TO_SAVE_INTERVAL = @"_time_to_24_hours_check";

if ([[NSDate date] timeIntervalSince1970] > [defaults doubleForKey:KEY_TO_SAVE_INTERVAL]){

    // One Day (24 hours) passed
    //YOUR_LABEL.text = @"YOUR TEXT";


}
else{
    // First time label text set
    //YOUR_LABEL.text = @"YOUR TEXT";
}

//Save time for every label text set

NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
[defaults setDouble:interval forKey:KEY_TO_SAVE_INTERVAL];
[defaults synchronize];