iOS NSNotification(通知)传值~demo

//联系人:石虎  QQ: 1224614774昵称:嗡嘛呢叭咪哄

/**

注意点: 1.看 GIF 效果图.

       2.看连线视图的效果图.

       3.看实现代码(直接复制实现效果).

*/

一、GIF 效果图:


二、连线视图的效果图:

图1:


图2:


三、实现代码:

=========================

===================================================

==========================


控制器1:

//  ViewController.m

//  通知测试 dome

//

//  Created by 石虎 on 2017/7/22.

//  Copyright © 2017 shihu. All rights reserved.

//


#import "ViewController.h"

#import "SHTwoViewController.h"


@interface ViewController ()


//显示传值的内容

@property (weak,nonatomic)IBOutletUILabel *textStrng;

//跳转

- (IBAction)notificationClick:(UIButton *)sender;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

   

    //注册通知----第一部

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(showLabelText:)name:@"labelTextNotification"object:nil];

}

//实现通知监听 ---第二部

- (void)showLabelText:(NSNotification *)notification

{

    //第三,实现通知中心内部的方法,并实现传值

    id text = notification.object;

    //赋值到文本上面

    self.textStrng.text = text;

}

//释放通知 ---第四部

- (void)dealloc

{

    //第四步,消息发送完,要移除掉

    [[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"labelTextNotification"object:nil];

}

#pragma mark -//点击通知跳转

- (IBAction)notificationClick:(UIButton *)sender{

    

    SHTwoViewController *twoVC =[[SHTwoViewControlleralloc]init];

    [selfpresentViewController:twoVCanimated:YEScompletion:nil];;

}

@end



=========================

===================================================

==========================

控制器2:

/  SHTwoViewController.m

//  通知测试 dome

//

//  Created by 石虎 on 2017/7/22.

//  Copyright © 2017 shihu. All rights reserved.

//


#import "SHTwoViewController.h"

#import "ViewController.h"


@interface SHTwoViewController ()

//输入要传值的文字

@property (weak,nonatomic)IBOutletUITextField *TextField;

//开始传值

- (IBAction)startValue:(id)sender;


@end


@implementation SHTwoViewController


- (void)viewDidLoad {

    [superviewDidLoad];

}

//开始传值

- (IBAction)startValue:(id)sender

{

    //发布通知----第三部

    [[NSNotificationCenterdefaultCenter]postNotificationName:@"labelTextNotification"object:self.TextField.text];


    [selfdismissViewControllerAnimated:YEScompletion:nil];


}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.viewendEditing:YES];

}

@end



谢谢!!!



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值