block傳值


                              block傳值

其實block傳值個人感覺跟代理很相似.也是從後往前傳.

//流程:
1.後一個界面定義一個block,並且定義一個屬性block
2.在後一個界面返回前一個界面的瞬間,(即:創建完成一個界面之後),調用block;
3.前一個界面實現block的實現
4.後一個界面在合適的機會, 讓(傳的值以參數的形式 含在block的參數裏)

代碼如下:


[objc] view plaincopy

  1. <pre name="code" class="objc">#import "FirstViewController.h"  

  2. #import "SecondViewController.h"  

  3. #import "UIButton+Create.h"  

  4. @interface FirstViewController ()  

  5. {  

  6.     UILabel * _label;  

  7. }  

  8. @end  

  9.   

  10. @implementation FirstViewController  

  11. - (void)dealloc  

  12. {  

  13.     [_label release];  

  14.     [super dealloc];  

  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.     self.view.backgroundColor = [UIColor redColor];  

  29.     self.navigationItem.title = @"首頁";  

  30.     self.view.userInteractionEnabled = YES;  

  31.     /** 

  32.      *  1.創建一個UIButton, 

  33.      *  2.並添加響應事件,從首頁跳轉到第二個頁面. 

  34.      */  

  35.     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(1001205050) title:@"Push" target:self action:@selector(didClickButtonAction)];  

  36.     button.userInteractionEnabled = YES;  

  37.     [self.view addSubview:button];  

  38.       

  39.       

  40.       

  41.     /** 

  42.      *  1.在第1個界面創建一個UILabel 

  43.      *  2.把第二頁輸入框輸入的字符串,通過block內部實現傳過來 

  44.      *  3.然後通過賦值給UILabel 

  45.      */  

  46.     _label = [[UILabel alloc]initWithFrame:CGRectMake(508020030)];  

  47.     _label.backgroundColor = [UIColor greenColor];  

  48.     [self.view addSubview:_label];  

  49.       

  50.     // Do any additional setup after loading the view.  

  51. }  

  52.   

  53. - (void)didClickButtonAction  

  54. {  

  55.       

  56.     /** 

  57.      *  1.用push的方法推出下一個頁面 

  58.      *  2.把第二頁輸入框輸入的字符串,通過block內部實現傳過來 

  59.      *  3.從而實現把輸入框輸入的字符串,傳到UILabel上. 

  60.      */  

  61.     SecondViewController * secondVC = [[SecondViewController alloc]init];  

  62.     secondVC.blocks=^(NSString * str){  

  63.           

  64.          _label.text = str;  

  65.     };  

  66.     [self.navigationController pushViewController:secondVC animated:YES];  

  67.     [secondVC release];  

  68. }  

  69.   

  70.   

  71.   

  72.   

  73. - (void)didReceiveMemoryWarning  

  74. {  

  75.     [super didReceiveMemoryWarning];  

  76.     // Dispose of any resources that can be recreated.  

  77. }  

  78.   

  79. @end  





[objc] view plaincopy

  1. <pre name="code" class="objc">#import <UIKit/UIKit.h>  

  2.   

  3. typedef void(^block)(NSString * str) ;  

  4. @interface SecondViewController : UIViewController  

  5. @property (nonatomic,copy)block blocks;  

  6. @end  





[objc] view plaincopy

  1. #import "SecondViewController.h"  

  2. #import "UIButton+Create.h"  

  3. #import "FirstViewController.h"  

  4. @interface SecondViewController ()  

  5. {  

  6.     UITextField * _textField;//創建一個輸入框  

  7. }  

  8. @end  

  9. @implementation SecondViewController  

  10.   

  11. - (void)dealloc  

  12. {  

  13.     [_textField release];  

  14.     [super dealloc];  

  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.     self.view.backgroundColor = [UIColor orangeColor];  

  29.     self.navigationItem.title = @"第二頁";  

  30.     /** 

  31.      *  1.創建一個UIButton, 

  32.      *  2.並添加響應事件,從第二個頁面返回到首頁. 

  33.      */  

  34.     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(1001205050) title:@"Back" target:self action:@selector(didClickButtonAction)];  

  35.     [self.view addSubview:button];  

  36.       

  37.     /** 

  38.      *  1.在第二個界面創建一個輸入框 

  39.      * 

  40.      */  

  41.     _textField = [[UITextField alloc]initWithFrame:CGRectMake(508020030)];  

  42.     _textField.borderStyle = UITextBorderStyleRoundedRect;  

  43.     [self.view addSubview:_textField];  

  44.       

  45.       

  46.       

  47.     // Do any additional setup after loading the view.  

  48. }  

  49.   

  50. - (void)didClickButtonAction  

  51. {  

[objc] view plaincopy

  1.    //調用block方法,把<span style="font-family: Arial;">_textField.text作爲參數傳過去</span>  

  2.   

  3.     _blocks(_textField.text);  

  4.     [self.navigationController popToRootViewControllerAnimated:YES];  

  5. }  

  6.   

  7. - (void)didReceiveMemoryWarning  

  8. {  

  9.     [super didReceiveMemoryWarning];  

  10.     // Dispose of any resources that can be recreated.  

  11. }  

  12.   

  13. @end  







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章