ios的通知应用

通知的传值:

 NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",self.playNum],@"playNum", nil];

    //创建通知

    NSNotification *notification =[NSNotification notificationWithName:@"playNumber" object:nil userInfo:dict];

    //通过通知中心发送通知

    [[NSNotificationCenter defaultCenter] postNotification:notification];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playNumAction:) name:@"playNumber" object:nil];






- (void)playNumAction:(NSNotification *)playNum {

    NSLog(@"%@",playNum.userInfo[@"playNum"]);

}

通知的应用:

[[NSNotificationCenter defaultCenter]

             postNotificationName:@"AddButton" object:self];



 [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(addButtonAction:)

                                                 name:@"AddButton" object:nil];


- (void)addButtonAction:(NSNotification *)addNotification {

    [self requestData];

}


以上的两种方法一个可用来传值,一个可用来监听事件,在ios的应用里面是非常方便的,用起来也是很高效的,但是注意的是要注意销毁通知,arc下可以在delloc中删除通知

你可能感兴趣的:(ios,NSNotification)