IOS 多种反向传值对比(代理,block,通知)
来源:互联网 发布:如何访问网页数据库 编辑:程序博客网 时间:2024/05/21 04:19
IOS中常用的3种传值方法,主要是是用于反向传值,分别为block,代理,通知。对于这三种传值大家一定要非常熟悉,尤其是对于初学者。今天写了一个demo讲3种传值放到一起。方便大家自己去体会到每种传值的特点。优缺点等。首先我们需要两个页面。分别为UIVIEWCONTROLLER.分别为AViewController和BViewController,A是首界面。我们需要把B界面的输入框内容传到A界面。下面分别看3种传值的实现方法
工程的文件结构如下图:
在AppDelegate.m中写A界面为入口
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.window.rootViewController = [[AViewController alloc]init]; [self.window makeKeyAndVisible]; return YES;}
A界面.h
#import <UIKit/UIKit.h>@interface AViewController : UIViewController//通知传值:接收方 定义接收通知的方法//-(void)receiveNotification:(NSString*)name;@end
A界面.m
#import "AViewController.h"#import "BViewController.h"//代理传值:代理方 第一件事 遵守代理协议@interface AViewController ()//<BViewControllerDelegate>@property(nonatomic)UILabel *label;@end@implementation AViewController//懒加载label-(UILabel *)label { if (_label == nil) { _label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)]; _label.backgroundColor = [UIColor yellowColor]; _label.font = [UIFont systemFontOfSize:24]; [self.view addSubview:_label]; } return _label;}- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self label];}//代理传值: 代理方 第二件事 实现协议中的代理方法//-(void)callbackVlaue:(NSString *)backValue {// self.label.text = backValue;//}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { BViewController *bvc = [[BViewController alloc]init]; [self presentViewController:bvc animated:YES completion:nil]; //代理传值: 代理方 第三件事 通知委托方 的代理人 为当前对象 // bvc.delegate = self; //block传值:实现block 接收block参数 实现传值 bvc.backblock = ^void(NSString *obj) { self.label.text = obj; }; //通知传值:接收方 告诉通知中心,接收BVC的通知// [self receiveNotification:@"BVC"];}//通知传值 :接收方 实现接收方法//-(void)receiveNotification:(NSString *)name {// // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:name object:nil];//}//通知传值:接收方 接收到通知后需要做的事//-(void)notification:(NSNotification*)notification {// NSDictionary *userInfo = notification.userInfo;// // self.label.text = userInfo[@"content"];//}@end
B界面.h:
#import <UIKit/UIKit.h>//block传值:typedef 一个blocktypedef void(^myblock)(NSString *);//代理传值:委托方第一件事,声明代理协议//@protocol BViewControllerDelegate <NSObject>//-(void)callbackVlaue:(NSString*)backValue;//@end@interface BViewController : UIViewController//通知传值:通知方 声明通知的方法//-(void)sendNotification:(NSString *)sendContent;//block传值:声明一个block属性@property (nonatomic,strong)myblock backblock;//代理传值:委托方第二件事,声明代理人属性 (协议类型)//@property(nonatomic,weak)id<BViewControllerDelegate> delegate;@end
B界面.m
#import "BViewController.h"@interface BViewController ()@property(nonatomic)UITextField *field;@end@implementation BViewController//懒加载field-(UITextField *)field { if (_field == nil) { _field = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 100)]; _field.font = [UIFont systemFontOfSize:24]; self.field.backgroundColor = [UIColor grayColor]; [self.view addSubview:_field]; } return _field;}- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self field];}//通知传值:通知方 实现发送通知的方法 发送名字为BVC 内容为参数sendContent的通知//-(void)sendNotification:(NSString *)sendContent//{// NSNotificationCenter *center = [NSNotificationCenter defaultCenter];// [center postNotificationName:@"BVC" object:self userInfo:@{@"content":sendContent}];// //}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //代理传值:委托方第三件事 在适当的实际通知代理人执行代理方法 // [self.delegate callbackVlaue:self.field.text]; //block传值:在适当的实际调用block self.backblock(self.field.text); //通知传值: 通知方 在适当的时机发送通知 内容为输入框中的文本// [self sendNotification:self.field.text]; [self dismissViewControllerAnimated:YES completion:nil];}@end
当前打开的为block传值。如需拷贝代码,实验3种传值,分别将对应的3种传值注释部分打开即可
1 0
- IOS 多种反向传值对比(代理,block,通知)
- iOS - block、代理、通知
- iOS 传值方法(属性传值、代理传值、Block、通知、单例)
- ios 代理、block和通知
- iOS---block反向传值
- iOS block反向传值
- Notification通知,通知中心,发布通知,通知代理对比 - iOS
- iOS 代理反向传值
- iOS 代理反向传值
- IOS代理反向传值
- iOS 代理反向传值
- iOS 代理反向传值
- iOS传值(属性传值,代理传值,单例传值,通知传值,Block)
- iOS 通知,代理,单例,block传值
- iOS知识点之一代理、通知、block
- iOS中 block 代理 通知的区别
- iOS中代理、通知、block的使用
- iOS --- block的一些使用方法(反向传值)
- LintCode 在O(1)时间复杂度删除链表节点
- 图论开篇
- ffmpeg源码分析一
- 如何生成验证码
- thinkPHP--Volist标签
- IOS 多种反向传值对比(代理,block,通知)
- ffmpeg源码分析二:main函数和transcode函数 (转2)
- Masonry 框架的使用
- Referenced file contains errors (xml文件第一行小红叉错误)
- AttributeError: 'SupervisedDataSet' object has no attribute '_convertToOneOfMany'
- 对象的生灭——构造函数、拷贝构造函数、赋值构造函数和析构函数
- java语言基础(17)——方法(类中的函数)的使用及重载
- ffmpeg源码分析三:transcode_init函数 (转3)
- Qt中窗体控件按照比例缩放,自适应窗口大小进行布局