美文网首页IOS知识整理
iOS Block的传值 代理传值 通知中心传值

iOS Block的传值 代理传值 通知中心传值

作者: iOS学末 | 来源:发表于2016-08-17 15:15 被阅读532次

有的时候,我们需要页面跳转传递数据,就需要将这个页面通过某种形式传递给另一个页面。我们把两个页面分别记做:传值页面 接受页面

某种形式传递包括:Block, 代理(delegate), 通知(Notification)

谁传值谁就设置代理 谁传值谁就设置Block

Block传值

1.使用Block属性实现回调传值
  • 在传值页面声明一个Block属性
typedef void(^sendValue)(NSString *context);
@interface SecondViewController : UIViewController
@property (copy,nonatomic) sendValue sendValueBlock;
  • 我们的传值页面里需要传值的地方调用Block方法这里是将传值页面中的textField的传递给了接收的页面
- (IBAction)sendButton:(UIButton *)sender 
{
    
    self.sendValueBlock(self.textFile.text);
   [self.navigationController popViewControllerAnimated:YES];
}
  • 在接收值得页面里实现Block
#import "FirstViewController.h"
@implementation FirstViewController
-(void)buttonAction:(UIButton *)button
{ 
   SecondViewController *secondVC = [[SecondViewController alloc]init]; 
   __weak FirstViewController *firstVC = self; 
   secondVC.sendValue = ^(NSString *str){ 
    firstVC.label .text = str; 
};
 [self.navigationController pushViewController:secondVC animated:YES];
}
2.方法中定义Block实现回调传值
  • 在传值页面 .h 文件申明Block属性
typedef void(^sendValue)(NSString *context);
  • 在传值页面 .h 文件声明Block方法
-(void)sendString:(NSString *)string andBlock: (sendValue)block;
  • 在 传值页面.m文件实现方法
-(void)sendString:(NSString *)string andBlock: (sendValue)block
{
   block(string);
}
  • 在接受页面实现方法
SecondViewController *secondVC = [[SecondViewController alloc]init]; 
[secondVC sendString:(NSString *)string  andBlock:^(NSString*string) {
 self.label.text = string;
}
];
block传值两种方法 有问题欢迎指正

2.代理传值

  • 第一步声明协议,声明协议方法
//设置代理 协议名的命名规范:类名+delegate
@protocol sendValueDelegate <NSObject>
//传值的内容作为协议方法的参数
- (void)sendString:(NSString *)string;
@end
  • 第二步添加代理人属性
@interface SecondViewController : UIViewController
@property (weak,nonatomic) id<sendValueDelegate> delegate;
  • 第三步让代理人执行协议方法
- (IBAction)backButton:(UIButton *)sender {
    //判断代理人是否存在,和是否实现了代理方法
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendString:)]) {
        [self.delegate sendString:self.textFile.text];
    }
   [self dismissViewControllerAnimated:YES completion:nil];
}
  • 第四步签订协议
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()<sendValueDelegate>
@end
  • 第五步指定代理人
#import "FirstViewController.h"
-(void)buttonAction:(UIButton *)button
{ 
  SecondViewController *secondVC = [[SecondViewController alloc]init]; 
  secondVC.delegate = self; 
  [self.navigationController pushViewController:secondVC animated:YES];
}
@end
  • 第六步实现协议方法
#import "FirstViewController.h"
-(void)sendString:(NSString *)string
{ 
  self.label.text = string;
}
@end

通知中心传值

  • NSNotificationCenter这个类是一个通知中心,使用单例设计,每个应用程序都会有一个默认的通知中心。用于监听某条广播。
  • NSNotification这个类可以理解为一个消息对象,其中有三个成员变量。
@property (readonly, copy) NSString *name;//这个成员变量是这个消息对象的唯一标识,用于辨别消息对象。
@property (readonly, retain) id object;//这个成员变量定义一个对象,可以理解为针对某一个对象的消息。
@property (readonly, copy) NSDictionary *userInfo;这个成员变量是一个字典,可以用其来进行传值。
示例代码

给接收值的页面注册一个消息通知注意:

  • observer: 观察者收到广播时候的回调方法,可以不带参数,也可以带参数,如果带参数,参数的类型为广播类型(NSNotification)
  • name:广播的名称object:如果发送的通知指定了
  • object对象,那么观察者接收的通知设置的object对象与其一样,才会接收到通知,但是接收通知如果将这个参数设置为了nil,则会接收一切通知。
#import "RootViewController.h"
@implementation RootViewController 
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotifi:) name:@"test" object:nil];
@end
receiveNotifi方法的实现
#import "RootViewController.h"
@implementation RootViewController
-(void)receiveNotifi:(NSNotification *)notifi
{    
    NSLog(@"object=====%@",notifi.object); 
    NSLog(@"userInfo=====%@",notifi.userInfo);
}
@end
第二步在要传值出去的界面发送通知消息
#import "SecondViewController.h"
@implementation SecondViewController
-(IBAction)button:(id)sender { 
[[NSNotificationCenter defaultCenter]postNotificationName:@"test" object:self.myTextField userInfo:@{@"title":self.myTextField.text}]; 
[self.navigationController popViewControllerAnimated:YES];
}
@end

文/Joker_King(简书作者)原文链接:http://www.jianshu.com/p/1b4d69e6cb4a著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

相关文章

  • iOS 常用传值方式

    总结 iOS 日常开发中的几种常用传值方式:正向传值代理传值block传值通知传值单例 文章代码:https://...

  • Block传值

    iOS传值一共有四种:属性传值,代理传值,通知传值以及Block传值; 今天我们来说一下Block传值: 概念:带...

  • iOS的五种传值

    前言 iOS常见的五种传值分别为属性传值,通知传值,代理传值,block传值,单例传值 属性传值 用于正向传值,简...

  • iOS 传值方法(属性传值、代理传值、Block、通知、单例)

    iOS 传值方法(属性传值、代理传值、Block、通知、单例)简单的介绍一下几个传值方式 1、属性传值 在传值的时...

  • ioS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • iOS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • iOS传值方式

    在iOS中,常见的传值方式有以下几种:1.属性传值2.单例传值3.通知传值4.代理传值5.Block这些传值方式,...

  • iOS传值的几种常用方式

    iOS常用的传值方式有以下几种: 属性传值、单例传值、代理传值、block传值、通知传值 接下来我就分别讲述一下这...

  • iOS 传值方式

    1,从前向后传值:属性传值 2, 从后向前传值: block、 代理、 通知 结论1, block、 代理 传值用...

  • iOS 传值的方式的区别

    1.属性传值。 2.block传值。 3.代理传值(delegate) 4.通知传值(notification)。...

网友评论

    本文标题:iOS Block的传值 代理传值 通知中心传值

    本文链接:https://www.haomeiwen.com/subject/saumsttx.html