类似于kvo通知机制的NSNotificationCenter

类似于kvo通知机制的NSNotificationCenter

IOS软件开始的时候总是会存在这种情况的,就是需要有些应用程序需要开后台任务,而且后台任务的处理需要一定的实际,例如是:文件的下载和上传,这是最费时,而且为了做到更好的用户体验,一般需要操作处理完毕之后,进行相关的提示,并通过刷新界面的方式,进行提示,这时候便是用到了IOS内置的NSNotifictionCenter,进行处理,(接下介绍kvo的应用环境),大家应该谨记 NsNotificationCenter是区别于NSNotification通知,两者不存在联系。

1.\定义需要使用到的回调通知函数,如下:

-(void)note

{

 UIAlertView *alert =[ [UIAlertView alloc] initWithTitle:@"文件下载完毕!"];

  [alert show];

}

2.定义通知:

[[NSNotificationCenter defaultCenter] addObserver:self seletor:@selector(note) name:@"download" object:nil];

3.调用通知:

-(void)getNotification{

  NSLog(@开始通知");

  [[NSNotificationCenter defaultCenter] postNotificatioName:@"back" object:self];

}

4.移除通知

-(void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"download" object:nil];

}

在调用通知的时候程序会在整个项目中寻找通知的名称,找到后发出请求,因此通知的名称在整个项目之中时唯一的。