zoukankan      html  css  js  c++  java
  • iOS 通知中心NSNotificationCenter 传值(用法)

    一、了解几个相关的类

    1、NSNotification

    这个类可以理解为一个消息对象,其中有三个成员变量。

    这个成员变量是这个消息对象的唯一标识,用于辨别消息对象。

    @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 把UITextfiled的text传到上一个控制器的UIlabel上显示出来

    #import "WBBViewController.h"
    
    @interface WBBViewController ()
    
    @property(nonatomic,strong)UITextField * textField;
    @end
    
    @implementation WBBViewController
    
    - (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 "WBBViewController.h"
    @interface ViewController ()
    
    @property (strong, 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 {
        
        WBBViewController * wbbVC=[[WBBViewController alloc]init];
        
        [self.navigationController pushViewController:wbbVC animated:YES];
        
        
    }
    

     

  • 相关阅读:
    es date_histogram强制补零
    macos下默认的调试工具是lldb
    test
    mybaity 代码自动生成器
    初始化的问题
    SQLServer常用语句
    PowerShell Install-Module 离线安装 .nupkg包
    .NET Core语句记录
    system design(how to design tweet)
    软件-开源
  • 原文地址:https://www.cnblogs.com/wangbinbin/p/4792004.html
Copyright © 2011-2022 走看看