转:关于对Blocks的实现代理传值的介绍

一、RootViewController:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import "RootViewController.h"  
  2. #import "SecondViewController.h"  
  3. @interface RootViewController ()  
  4. {  
  5.     UILabel *_myLabel;  
  6.      
  7. }  
  8. @end  
  9.   
  10. @implementation RootViewController  
  11. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  12. {  
  13.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  14.     if (self) {  
  15.         // Custom initialization  
  16.     }  
  17.     return self;  
  18. }  
  19.   
  20. - (void)viewDidLoad  
  21. {  
  22.     [super viewDidLoad];  
  23.     self.title = @"第一页";  
  24.     UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage)];  
  25.     self.navigationItem.rightBarButtonItem = item;  
  26.       
  27.     _myLabel = [[UILabel alloc]initWithFrame:CGRectMake(010032050)];  
  28.     _myLabel.textAlignment = NSTextAlignmentCenter;  
  29.     _myLabel.text = @"Blocks";  
  30.     [self.view addSubview:_myLabel];  
  31.     // Do any additional setup after loading the view from its nib.  
  32. }  
  33. -(void)nextPage{  
  34.     SecondViewController *second = [[SecondViewController alloc]initWithBlock:^(NSString *str) {  
  35.         NSLog(@"%@",str);  
  36.         _myLabel.text = str;  
  37.     }];  
  38.     [self.navigationController pushViewController:second animated:YES];  
  39. }  
  40. - (void)didReceiveMemoryWarning  
  41. {  
  42.     [super didReceiveMemoryWarning];  
  43.     // Dispose of any resources that can be recreated.  
  44. }  
  45.   
  46. @end  


二、SecondViewConroller:

.h文件

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2. typedef void(^myBlock)(NSString *);  
  3.   
  4. @interface SecondViewController : UIViewController  
  5. {  
  6.     myBlock block;  
  7. }  
  8. -(id)initWithBlock:(myBlock)str;  
  9. @end  

.m文件

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import "SecondViewController.h"  
  2.   
  3. @interface SecondViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation SecondViewController  
  8. -(id)initWithBlock:(myBlock)str{  
  9.     self = [super init];  
  10.     if(self)  
  11.     {  
  12.         block = str;  
  13.     }  
  14.     return self;  
  15. }  
  16. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  17. {  
  18.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  19.     if (self) {  
  20.         // Custom initialization  
  21.     }  
  22.     return self;  
  23. }  
  24.   
  25. - (void)viewDidLoad  
  26. {  
  27.     [super viewDidLoad];  
  28.       
  29.     UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  30.     myButton.frame = CGRectMake(10010010050);  
  31.     [myButton setTitle:@"点我传值!" forState:UIControlStateNormal];  
  32.     [myButton addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];  
  33.     [self.view addSubview:myButton];  
  34.       
  35.     // Do any additional setup after loading the view from its nib.  
  36. }  
  37. -(void)clicked{  
  38.     NSLog(@"我被点击了!");  
  39.       
  40.     if (block) {  
  41.         block(@"哈哈");  
  42.     }  
  43.     //[self.navigationController popViewControllerAnimated:YES];  
  44. }  
  45.   
  46. - (void)didReceiveMemoryWarning  
  47. {  
  48.     [super didReceiveMemoryWarning];  
  49.     // Dispose of any resources that can be recreated.  
  50. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值