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

96 
Joker_King 
2016.04.18 22:17*  字数 802  阅读 1792 评论 7

在我们需要在另一个页面中获取到这个页面的数据的时候,我们就需要将这个页面的数据通过某种形式传递给另一个页面。在这里我们将这两个页面分别称为,传值页面和接收页面。今天介绍的传值的方式多用于从后往前传值。

Block传值

Block传值的两种方式

- 使用Block属性实现回调传值

在Block传值中传值页面和接收页面两者之间的关系是,在接收页面我们在点击Button按钮的时候,导航控制器推出传值页面,在传值的页面点击Button按钮时会pop出接收页面,Block传值的过程就在这里完成。

  • 第一步在我们的传值的页面声明一个Block属性
#import <UIKit/UIKit.h>
typedef void(^sendValue)(NSString *value);
@interface SecondViewController : UIViewController
@property (nonatomic, copy)sendValue sendValueBlock;
@end
  • 第二步在我们的传值页面里需要传值的地方调用Block方法
    这里是将传值页面中的textField的值传递给了接收的页面
#import "SecondViewController.h"
@implementation SecondViewController
-(void)buckAction:(UIBarButtonItem *)back{
    self.sendValue(self.textField.text);
    [self.navigationController popViewControllerAnimated:YES];
}
@end
  • 在接收值的页面里实现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];
}
@end

- 在方法中定义Block实现回调传值

  • 第一步在AppTool.h中重定义void(^)(NSString *string)类型名别为AppToolBlock
AppTool.h
typedef void(^AppToolBlock)(NSString *string);
  • 第二步声明方法,在方法中封装Block
AppTool.h
-(void)sendNumber:(NSInteger )number andBlock:(AppToolBlock)block;
  • 第三步在AppTool.m实现方法,并执行Block
-(void)sendNumber:(NSInteger )number andBlock:(AppToolBlock)block{
 NSString *string = [NSStringstringWithFormat:@"%ld",number];
 block(string);
}
  • 第四步在需要接收值的地方调用该方法
#import "SecondViewController.h"
@implementation SecondViewController
AppTool *appTool = [[AppTool alloc] init];
[appTool sendNumber:216541 andBlock:^(NSString*string) { 
self.label.text = string;
}];
@end

至此Block的两种传值的方法已经介绍完毕,欢迎指正。

协议传值

协议传值共分为六步

  • 第一步声明协议,声明协议方法
#import <UIKit/UIKit.h>
//协议名的命名规范:类名+delegate
@protocol SecondDelegate <NSObject>
//传值的内容作为协议方法的参数
-(void)passString:(NSString *)string;
@end
  • 第二步添加代理人属性
@interface SecondViewController : UIViewController
@property(nonatomic,weak)id<SecondDelegate>delegate;
//用assign,weak是为了防止产生保留环,造成无法释放
@end
  • 第三步让代理人执行协议方法
#import "SecondViewController.h"
-(void)buckAction:(UIBarButtonItem *)back{
    if (self.delegate && [self.delegate respondsToSelector:@selector(passString:)]) {
//判断代理人是否存在,和是否实现了代理方法
        [self.delegate passString:self.textField.text];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
@end
  • 第四步签订协议
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()<SecondDelegate>
@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)passString:(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
  • 注意:在用通知中心传值的时候,需要保证参数中的广播名称是一致的。
    至此通知中心传值的方法已经介绍完毕,欢迎指正。

猜你喜欢

转载自blog.csdn.net/Alexander_Wei/article/details/78365040
今日推荐