通知傳值
阿新 • • 發佈:2018-12-17
通知傳值 ( 多傳送 , 1接收) 三個通知方: 第一個 NSDictionary *dic = @{@"title":TextField.text,@"typeID":@(1)}; [[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil userInfo:dic]; 1 2 第二個 NSDictionary *dic = @{@"title":textArr[indexPath.row],@"typeID":@(2)}; [[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil userInfo:dic]; 1 2 第三個 NSDictionary *dic = @{@"title":TextField.text,@"typeID":@(3)}; [[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil userInfo:dic]; 1 2 接收方 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti:) name:@"noti" object:nil]; -(void)noti:(NSNotification *)noti { NSDictionary *dic = [noti userInfo]; NSString * title = dic[@"title"]; if ([dic[@"typeID"] integerValue]==1) { //Name _nichengString = title; } if ([dic[@"typeID"] integerValue]==2) { //GongSi _gongsiString2 = title; } if ([dic[@"typeID"] integerValue]==3) { //nicheng _xingmingString = title; } //重新整理表格 [_tbv reloadData]; }
原 通知傳值(1傳送 , 1接收) 需要兩個介面 一個為接受介面(介面1) 一個為傳送通知介面(介面2) 介面2傳送通知 , 介面1 接受通知 獲取資料完成傳值 介面2程式碼 NSDictionary *dic = [NSDictionary dictionaryWithObject:@"我是介面2的值" forKey:@"title"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"noti" object:nil userInfo:dic]; [self.navigationController popViewControllerAnimated:YES]; 1 2 3 4 介面1的程式碼 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti:) name:@"noti" object:nil]; -(void)noti:(NSNotification *)noti { //使用userInfo處理訊息 NSDictionary *dic = [noti userInfo]; NSString *info = [dic objectForKey:@"title"]; self.label.text = info; } -(void)dealloc { //移除觀察者,Observer不能為nil [[NSNotificationCenter defaultCenter] removeObserver:self]; }