代理传值

来源:互联网 发布:网络经常听的dj 编辑:程序博客网 时间:2024/04/30 15:42
  1. #import "FirstViewController.h"  
  2. #import "SecondViewController.h"  
  3. @interface FirstViewController ()  
  4. {  
  5.     UILabel * _label;  
  6. }  
  7. @end  
  8.   
  9. @implementation FirstViewController  
  10. - (void)dealloc  
  11. {  
  12.     [_label release];  
  13.     [super dealloc];  
  14. }  
  15. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  16. {  
  17.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  18.     if (self) {  
  19.         // Custom initialization  
  20.     }  
  21.     return self;  
  22. }  
  23.   
  24. - (void)viewDidLoad  
  25. {  
  26.     [super viewDidLoad];  
  27.     self.view.backgroundColor = [UIColor redColor];  
  28.     self.navigationItem.title = @"首页";  
  29.     self.view.userInteractionEnabled = YES;  
  30.     /** 
  31.      *  1.创建一个UIButton, 
  32.      *  2.并添加响应事件,从首页跳转到第二个页面. 
  33.      */  
  34.     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(1001205050) title:@"Push" target:self action:@selector(didClickButtonAction)];  
  35.     button.userInteractionEnabled = YES;  
  36.     [self.view addSubview:button];  
  37.       
  38.       
  39.       
  40.     /** 
  41.      *  1.在第1个界面创建一个UILabel 
  42.      *  2.把第二页输入框输入的字符串,通过代理方法传过来 
  43.      *  3.然后通过赋值给UILabel 
  44.      */  
  45.     _label = [[UILabel alloc]initWithFrame:CGRectMake(508020030)];  
  46.     _label.backgroundColor = [UIColor greenColor];  
  47.     [self.view addSubview:_label];  
  48.       
  49.     // Do any additional setup after loading the view.  
  50. }  
  51.   
  52. - (void)didClickButtonAction  
  53. {  
  54.       
  55.     /** 
  56.      *  1.用push的方法推出下一个页面 
  57.      *  2.把第二页输入框输入的字符串,通过代理方法传过来 
  58.      *  3.从而实现把首页输入框输入的字符串,传到第二页的UILabel上. 
  59.      */  
  60.     SecondViewController * secondVC = [[SecondViewController alloc]init];  
  61.     secondVC.delegate = self;//确认代理  
  62.     [self.navigationController pushViewController:secondVC animated:YES];  
  63.     [secondVC release];  
  64. }  
  65.   
  66. //实现代理方法  
  67. - (void)passByValueSecondVC:(SecondViewController *)secondVC text:(NSString *)text  
  68. {  
  69.     _label.text = text;  
  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在CODE上查看代码片派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2. @class SecondViewController;  
  3. @protocol secondViewControllerDelegate <NSObject>  
  4.   
  5. - (void)passByValueSecondVC:(SecondViewController *)secondVC text:(NSString *)text;  
  6.   
  7. @end  
  8.   
  9. @interface SecondViewController : UIViewController  
  10. @property (nonatomic,assign)id<secondViewControllerDelegate>delegate;  
  11. @end  


[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  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. {  
  52.     //调用代理方法,_textField.text传值  
  53.     [_delegate passByValueSecondVC:self text:_textField.text];  
  54.     [self.navigationController popToRootViewControllerAnimated:YES];  
  55. }  
  56.   
  57. - (void)didReceiveMemoryWarning  
  58. {  
  59.     [super didReceiveMemoryWarning];  
  60.     // Dispose of any resources that can be recreated.  
  61. }  
  62.   
  63. @end 
0 0
原创粉丝点击