NSNotificationCenter 通知中心傳值


1NSNotification

這個類可以理解為一個消息對象,其中有三個成員變量。

這個成員變量是這個消息對象的唯一標識,用於辨別消息對象。

@property (readonly, copy) NSString *name;

 

這個成員變量定義一個對象,可以理解為針對某一個對象的消息。

@property (readonly, retain) id object;

 

這個成員變量是一個字典,可以用其來進行傳值。

@property (readonly, copy) NSDictionary *userInfo;

 幾點注意:

1、如果發送的通知指定了object對象,那么觀察者接收的通知設置的object對象與其一樣,才會接收到通知,但是接收通知如果將這個參數設置為了nil,則會接收一切通知。

2、觀察者的SEL函數指針可以有一個參數,參數就是發送的死奧西對象本身,可以通過這個參數取到消息對象的userInfo,實現傳值。

 

首先我們要確認那邊要傳值那邊要接受傳過來的值,

在傳值的一方我們要寫一個創建一個消息 NSNotification ,並且用通知中心NSNotificationCenter 發送這個消息

接收傳過來的值這里我們要創建一個通知中心NSNotificationCenter 並且添加觀察者,在添加觀察者這個方法里面有一個響應事件的方法  我們可以在響應事件的方法里面接收傳過來的值

 

好啦我們開始寫代碼吧

傳值的一方在這個控制器里面我們創建一個UITextFiled UITextfiledtext傳到上一個控制器的UIlabel上顯示出來

#import "PYJViewController.h"

@interface PYJViewController ()
@property (nonatomic,strong) UITextField *textField;
@end

@implementation PYJViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton * button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame =CGRectMake(100, 100, 100, 30);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    button.backgroundColor=[UIColor yellowColor];
    [button addTarget:self action:@selector(backLastPage:) forControlEvents:UIControlEventTouchUpInside];
    self.textField=[[UITextField alloc]init];
    self.textField.frame=CGRectMake(100, 150, 200, 30);
    self.textField.borderStyle=UITextBorderStyleRoundedRect;

    [self.view addSubview:self.textField];
    
    [self.view addSubview:button];
    self.view.backgroundColor=[UIColor whiteColor];

}

- (void)backLastPage:(UIButton *)bt{
    
    //創建一個消息對象
    NSNotification * notice =[NSNotification notificationWithName:@"notice" object:nil userInfo:@{@"text":self.textField.text}];
    //發送消息
    [[NSNotificationCenter defaultCenter]postNotification:notice];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

 

接收傳值的控制器:

#import "ViewController.h"
#import "PYJViewController.h"
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)buttonAction:(UIButton *)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.label.text=@"測試一";
    
    //獲取通知中心
    NSNotificationCenter * center =[NSNotificationCenter defaultCenter];
    
    //添加觀察者 Observer表示觀察者  reciveNotice:表示接收到的消息  name表示再通知中心注冊的通知名  object表示可以相應的對象 為nil的話表示所有對象都可以相應
    [center addObserver:self selector:@selector(reciveNotice:) name:@"notice" object:nil];
    
}

- (void)reciveNotice:(NSNotification *)notification{
    
    NSLog(@"收到消息啦!!!");
    
    self.label.text=[notification.userInfo objectForKey:@"text"];
    
}

- (IBAction)buttonAction:(UIButton *)sender {
    
    PYJViewController * vc=[[WBBViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM