OBjective-C中的Notificationcenter

当用户修改MAC系统的时间时,通知程序。

在.m文件中加入方法:

#import "BNRLogger.h"

@implementation BNRLogger
- (NSString *)lastTimerString{
    static NSDateFormatter *dateFormatter = nil;
    if(!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        NSLog(@"created dateFormatter");
    }
    return [dateFormatter stringFromDate:self.lastTime];
}
- (void)updateLastTimer:(NSTimer *)t {
    NSDate *now = [NSDate date];
    [self setLastTime:now];
    NSLog(@"I want to see the timer here: ");
    NSLog(@"Just set time to %@", self.lastTimerString);
    
}

- (void)zoneChange:(NSNotification *)note{
    NSLog(@"The system time zone has changed!");
}

@end

在main函数中注册并调用:

#import <Foundation/Foundation.h>
#import "BNRLogger.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        BNRLogger *logger = [[BNRLogger alloc] init];
        [[NSNotificationCenter defaultCenter]
         addObserver:logger
         selector:@selector(zoneChange:)
         name:NSSystemTimeZoneDidChangeNotification
         object:nil];
    __unused   NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0
                                                          target:logger
                                                        selector:@selector(updateLastTimer:)
                                                        userInfo:nil
                                                         repeats:YES];
        
        [[NSRunLoop currentRunLoop] run];
    }
    return 0;
}

查看log:

2018-03-14 22:31:01.156010+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:01.156324+0800 TOCRunloopa[78189:4808386] created dateFormatter

2018-03-14 22:31:01.159161+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:01

2018-03-14 22:31:03.156022+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:03.156175+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:03

2018-03-14 22:31:05.156034+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:05.156163+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:05

2018-03-14 22:31:07.155929+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:07.156086+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:07

2018-03-14 22:31:09.155590+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:09.155950+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:09

2018-03-14 22:31:11.154851+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:11.155819+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:11

2018-03-14 22:31:13.156983+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:13.157159+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:13

2018-03-14 22:31:15.153715+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:15.153921+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:15

2018-03-14 22:31:17.155811+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:17.156208+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:17

2018-03-14 22:31:19.157613+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:19.158116+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:19

2018-03-14 22:31:20.693787+0800 TOCRunloopa[78189:4808386] The system time zone has changed!

2018-03-14 22:31:21.155836+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:21.161952+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:21

2018-03-14 22:31:23.157004+0800 TOCRunloopa[78189:4808386] I want to see the timer here:

2018-03-14 22:31:23.157178+0800 TOCRunloopa[78189:4808386] Just set time to 2018314 下午10:31:23



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值