回传值问题,一直都是困扰初学者的问题,今写者 代理、通知、block 三者的回传值做了一个小小的总结,
Main.storyboard 视图:
通过代码分别创建三个代表 代理、通知、block 的按钮,点击相应的按钮,会将相应的文本传入文本框中显示出来
代码如下:
1 // GWFMyDelegateBlockNotyView.h 2 // 回传值 3 4 #import <UIKit/UIKit.h> 5 @class GWFMyDelegateBlockNotyView; 6 7 //协议 8 @protocol GWFMyDelegateBlockNotyViewDelegate <NSObject> 9 10 //定义代理方法 11 -(void)GWFMyDelegateBlockNotyView:(GWFMyDelegateBlockNotyView *)view andString:(NSString *)string andImage:(UIImage *)image; 12 13 @end 14 15 @interface GWFMyDelegateBlockNotyView : UIView 16 17 //定义 id 属性 18 @property (nonatomic,weak) id <GWFMyDelegateBlockNotyViewDelegate> myDelegate; 19 20 //添加 block 属性 21 @property (nonatomic,copy) void (^myBlock)(NSString *,UIImage *); 22 23 @end
1 // GWFMyDelegateBlockNotyView.m 2 // 回传值 3 4 #import "GWFMyDelegateBlockNotyView.h" 5 #import "SVProgressHUD.h" 6 #import "MBProgressHUD+Ex.h" 7 8 @implementation GWFMyDelegateBlockNotyView 9 10 - (instancetype)init 11 { 12 self = [super init]; 13 if (self) { 14 15 #pragma ==================================== 代理传值按钮 16 17 //1.创建 delegateBtn 18 UIButton *deleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 19 //设置背景颜色 20 deleBtn.backgroundColor = [UIColor redColor]; 21 //设置title 22 [deleBtn setTitle:@"代理传值" forState:UIControlStateNormal]; 23 //设置 title 的颜色 24 [deleBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 25 26 //添加到控制器上 27 [self addSubview:deleBtn]; 28 29 //添加 deleBtn 的监听事件 30 [deleBtn addTarget:self action:@selector(deleBtnClick) forControlEvents:UIControlEventTouchUpInside]; 31 32 #pragma ==================================== Block传值按钮 33 34 //2.创建 Block 按钮 35 UIButton *blockBtn = [[UIButton alloc] initWithFrame:CGRectMake(110, 0, 100, 100)]; 36 //设置背景颜色 37 blockBtn.backgroundColor = [UIColor greenColor]; 38 //设置 title 39 [blockBtn setTitle:@"Block传值" forState:UIControlStateNormal]; 40 //设置 title 的颜色 41 [blockBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 42 43 //添加到控制器上 44 [self addSubview:blockBtn]; 45 46 //添加 blockBtn 的监听事件 47 [blockBtn addTarget:self action:@selector(blockBtnClick) forControlEvents:UIControlEventTouchUpInside]; 48 49 #pragma ==================================== 通知传值按钮 50 51 //3.创建 notyBtn 52 UIButton *notyBtn = [[UIButton alloc] initWithFrame:CGRectMake(215, 0, 100, 100)]; 53 //设置背景颜色 54 notyBtn.backgroundColor = [UIColor blueColor]; 55 //设置 title 56 [notyBtn setTitle:@"通知传值" forState:UIControlStateNormal]; 57 //设置 notyBtn 的 title 的颜色 58 [notyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 59 60 //添加到控制器上 61 [self addSubview:notyBtn]; 62 63 //添加 notyBtn 的监听事件 64 [notyBtn addTarget:self action:@selector(notyBtnClick) forControlEvents:UIControlEventTouchUpInside]; 65 66 } 67 return self; 68 } 69 70 //实现按钮的点击事件 71 #pragma mark -------代理 72 73 -(void)deleBtnClick { 74 NSString *myText = @"这是代理传来的值"; 75 UIImage *image = [UIImage imageNamed:@"0 (3)"]; 76 77 //判断代理 78 if ([self.myDelegate respondsToSelector:@selector(GWFMyDelegateBlockNotyView:andString:andImage:)]) { 79 80 //执行代理 81 [self.myDelegate GWFMyDelegateBlockNotyView:self andString:myText andImage:image]; 82 83 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 84 85 [SVProgressHUD showWithStatus:@"正在连接..." maskType:SVProgressHUDMaskTypeBlack]; 86 }); 87 88 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 89 90 [SVProgressHUD dismiss]; 91 UIApplication *app = [UIApplication sharedApplication]; 92 NSURL *url = [NSURL URLWithString:@"http://www.dianping.com"]; 93 [app openURL:url]; 94 95 }); 96 } 97 } 98 99 #pragma mark -------block 100 101 -(void)blockBtnClick { 102 NSString *myText = @"这是Block传来的值"; 103 UIImage *image = [UIImage imageNamed:@"0 (2)"]; 104 self.myBlock(myText,image); 105 106 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 107 108 [SVProgressHUD showWithStatus:@"正在连接..." maskType:SVProgressHUDMaskTypeBlack]; 109 }); 110 111 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 112 113 [SVProgressHUD dismiss]; 114 115 UIApplication *app = [UIApplication sharedApplication]; 116 NSURL *url = [NSURL URLWithString:@"http://www.10010.com"]; 117 [app openURL:url]; 118 119 }); 120 } 121 122 #pragma mark --------通知 123 124 -(void)notyBtnClick { 125 NSString *myText = @"这是通知传来的值"; 126 UIImage *image = [UIImage imageNamed:@"0 (7)"]; 127 128 //发布一个通知 129 [[NSNotificationCenter defaultCenter] postNotificationName:@"myNoty" object:nil userInfo:@{@"myText":myText,@"image":image}]; 130 131 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 132 133 [SVProgressHUD showWithStatus:@"正在连接..." maskType:SVProgressHUDMaskTypeBlack]; 134 }); 135 136 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 137 138 [SVProgressHUD dismiss]; 139 140 UIApplication *app = [UIApplication sharedApplication]; 141 NSURL *url = [NSURL URLWithString:@"http://www.youku.com"]; 142 [app openURL:url]; 143 144 }); 145 } 146 147 @end
控制器中:
1 // ViewController.m 2 // 回传值 3 4 #import "ViewController.h" 5 #import "GWFMyDelegateBlockNotyView.h" 6 7 @interface ViewController ()<GWFMyDelegateBlockNotyViewDelegate> 8 9 @property (weak, nonatomic) IBOutlet UILabel *myLabel; 10 @property (weak, nonatomic) IBOutlet UIImageView *myImageView; 11 12 @end 13 14 @implementation ViewController 15 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 19 self.view.backgroundColor = [UIColor orangeColor]; 20 //添加 myView 21 GWFMyDelegateBlockNotyView *myView = [[GWFMyDelegateBlockNotyView alloc] init]; 22 myView.frame = CGRectMake(30, 150, 300, 200); 23 [self.view addSubview:myView]; 24 25 //设置代理 26 myView.myDelegate = self; 27 28 //block 29 myView.myBlock = ^(NSString *block,UIImage *myBlock){ 30 self.myLabel.text = block; 31 self.myImageView.image = myBlock; 32 }; 33 34 //添加通知的一个监听事件 35 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noty:) name:@"myNoty" object:nil]; 36 37 } 38 39 //实现代理方法 40 -(void)GWFMyDelegateBlockNotyView:(GWFMyDelegateBlockNotyView *)view andString:(NSString *)string andImage:(UIImage *)image { 41 self.myLabel.text = string; 42 self.myImageView.image = image; 43 } 44 45 //实现通知的监听方法 46 -(void)noty:(NSNotification *)noty { 47 self.myLabel.text = noty.userInfo[@"myText"]; 48 self.myImageView.image = noty.userInfo[@"image"]; 49 } 50 51 @end
执行结果:
1> 点击代理按钮后:
2> 点击通知按钮后:
3>点击block按钮后:
时间: 2024-10-24 17:25:53