iOS 通知的基本用法

来源:互联网 发布:阿莫源码社区 编辑:程序博客网 时间:2024/05/15 09:26

一、首先要注册通知

#import "RootViewController.h"@interface RootViewController (){    NSTimer *_timer;//添加定时器}@end@implementation RootViewController- (void)viewDidLoad {    [super viewDidLoad];    //注册通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack) name:@"back" object:nil];    [self creatNStimer];}

启用定时器

-(void)creatNStimer{    NSLog(@"get it");    _timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(Notification) userInfo:nil repeats:NO];    //发出通知}

二、执行通知

-(void)Notification{    //发出通知    [[NSNotificationCenter defaultCenter] postNotificationName:@"back" object:self];}-(void)callBack{    //执行通知    NSLog(@"this is Notification");}

三、移除通知

-(void)dealloc{    //移除通知    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"back" object:nil];}
0 0