zoukankan      html  css  js  c++  java
  • 通知传值(NSNotificationCenter)


    通知传值

    //流程:
    1.
    注冊通知
    2.
    通知中心,发送一条消息通知----------当中name名字千万不要写错了,会出如今3个地方
    3.
    实现通知中心内部的方法,并实现传值
    4.
    第四步,消息发送完,要移除掉

    代码例如以下:

    #import "FirstViewController.h"
    #import "SecondViewController.h"
    #import "UIButton+Create.h"
    @interface FirstViewController ()
    {
        UILabel * _label;
    }
    @end
    
    @implementation FirstViewController
    - (void)dealloc
    {
        [_label release];
        //第四步,消息发送完,要移除掉
        [[NSNotificationCenter defaultCenter]removeObserver:self name:@"labelTextNotification" object:nil];
        [super dealloc];
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        
        self.view.backgroundColor = [UIColor redColor];
        self.navigationItem.title = @"首页";
        
        _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
        _label.backgroundColor = [UIColor greenColor];
        [self.view addSubview:_label];
        
        
        
        UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)];
        [self.view addSubview:button];
        
        //第二步,通知中心,发送一条消息通知----------当中name名字千万不要写错了,会出如今3个地方
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showLabelText:) name:@"labelTextNotification" object:nil];
        
     
        
        
    	// Do any additional setup after loading the view.
    }
    
    - (void)showLabelText:(NSNotification *)notification
    {
        //第三,实现通知中心内部的方法,并实现传值
        id text = notification.object;
        _label.text = text;
    }
    
    - (void)didClickButtonAction
    {
        
      
        SecondViewController * secondVC = [[SecondViewController alloc]init];
        [self.navigationController pushViewController:secondVC animated:YES];
        [secondVC release];
    }
    
    - (void)didClick:(NSString *)text
    {
        _label.text = text;
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    #import "SecondViewController.h"
    #import "FirstViewController.h"
    #import "UIButton+Create.h"
    @interface SecondViewController ()
    {
        UITextField * _textField;//创建一个输入框
    }
    @end
    @implementation SecondViewController
    
    - (void)dealloc
    {
        [_textField release];
        [super dealloc];
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor orangeColor];
        self.navigationItem.title = @"第二页";
      
        
        
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:_textField];
        
        
        
        
        UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Back" target:self action:@selector(didClickButtonAction)];
        [self.view addSubview:button];
     
        
        
        
        
    	// Do any additional setup after loading the view.
    }
    
    - (void)didClickButtonAction
    {
        
        //第一步注冊通知
        [[NSNotificationCenter defaultCenter]postNotificationName:@"labelTextNotification" object:_textField.text];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    




  • 相关阅读:
    匿名变量
    Vue父子组件传值与非父子传值
    TCP三次握手分析
    @media screen 响应式布局
    H5新增多媒体标签
    npm+node+vue配置一套带走
    vue+echarts全国疫情地图
    js本地时间格式化
    vue iview分页
    Vue打包后访问静态资源路径问题
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6773615.html
Copyright © 2011-2022 走看看