ios中几种传值方式

来源:互联网 发布:手机淘宝账号交易平台 编辑:程序博客网 时间:2024/06/05 11:41

Target-Action传值

实质就是:A页面要给B页面传值,A就提供接口出去,抓A到B内部来,A间接调用自己内部方法(相当于,A把自己内部需                     要操作的方法,传到B内来,到B内部进行赋值,这样就不存在访问不到各自的局部实例变量)

      @property (nonatomic,assign)id traget;  @property (nonatomic,assign)SEL action;

       [self.traget performSelector:self.action withObject:nil];(是否需要传参数自己定,最多2个)

代码如下:

[objc] view plaincopy
  1. #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.       
  29.       
  30.     self.view.backgroundColor = [UIColor redColor];  
  31.     self.navigationItem.title = @"首页";  
  32.       
  33.     _label = [[UILabel alloc]initWithFrame:CGRectMake(508020030)];  
  34.     _label.backgroundColor = [UIColor greenColor];  
  35.       
  36.     //    _label.text = self.text;  
  37.     [self.view addSubview:_label];  
  38.       
  39.       
  40.       
  41.     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(1001205050) title:@"Push" target:self action:@selector(didClickButtonAction)];  
  42.     [self.view addSubview:button];  
  43.       
  44.       
  45.     // Do any additional setup after loading the view.  
  46. }  
  47.   
  48. - (void)didClickButtonAction  
  49. {  
  50.       
  51.     
  52.     SecondViewController * secondVC = [[SecondViewController alloc]init];  
  53.     secondVC.target = self;  
  54.     secondVC.action = @selector(didClick:);  
  55.     [self.navigationController pushViewController:secondVC animated:YES];  
  56.     [secondVC release];  
  57. }  
  58.   
  59. - (void)didClick:(NSString *)text  
  60. {  
  61.     _label.text = text;  
  62. }  
  63.   
  64. - (void)didReceiveMemoryWarning  
  65. {  
  66.     [super didReceiveMemoryWarning];  
  67.     // Dispose of any resources that can be recreated.  
  68. }  
  69.   
  70. @end  

[objc] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface SecondViewController : UIViewController  
  4.   
  5. @property (nonatomic,assign)id target;  
  6. @property (nonatomic,assign)SEL action;  
  7. @end  

[objc] view plaincopy
  1. #import "SecondViewController.h"  
  2. #import "FirstViewController.h"  
  3. #import "UIButton+Create.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.       
  32.       
  33.     _textField = [[UITextField alloc]initWithFrame:CGRectMake(508020030)];  
  34.     _textField.borderStyle = UITextBorderStyleRoundedRect;  
  35.     [self.view addSubview:_textField];  
  36.       
  37.       
  38.       
  39.       
  40.     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(1001205050) title:@"Back" target:self action:@selector(didClickButtonAction)];  
  41.     [self.view addSubview:button];  
  42.    
  43.       
  44.       
  45.       
  46.       
  47.     // Do any additional setup after loading the view.  
  48. }  
  49.   
  50. - (void)didClickButtonAction  
  51. {  
  52.     [_target performSelector:_action withObject:_textField.text];  
  53.     [self.navigationController popToRootViewControllerAnimated:YES];  
  54. }  
  55.   
  56.   
  57.   
  58. - (void)didReceiveMemoryWarning  
  59. {  
  60.     [super didReceiveMemoryWarning];  
  61.     // Dispose of any resources that can be recreated.  
  62. }  
  63.   
  64. @end  



 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  




属性/方法传值

//1.后面的界面定义了一个属性,用于保存,前一个界面,传过来的值

//注:属性定义成字符串还是别的类型,取决于你的需求,本例我们需要一个字符串,用于UILabel显示

//2.后面的界面创建完毕之后,为属性赋值,(即:记录需要传递的值)

//3.在需要使用值的地方,使用属性记录的值这种通过定义属性,达到传值的方式,称为属性传值,

//属性传值,一般用于从前一个界面向后一个界面传值;

代码如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #import "FirstViewController.h"  
  2. #import "SecondViewController.h"  
  3. #import "UIButton+Create.h"  
  4. @interface FirstViewController ()  
  5. {  
  6.     UITextField * _textField;//创建一个输入框  
  7. }  
  8. @end  
  9.   
  10. @implementation FirstViewController  
  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.       
  29.       
  30.     self.view.backgroundColor = [UIColor redColor];  
  31.     self.navigationItem.title = @"首页";  
  32.     /** 
  33.      *  1.在第一个界面创建一个输入框 
  34.      *   
  35.      */  
  36.     _textField = [[UITextField alloc]initWithFrame:CGRectMake(508020030)];  
  37.     _textField.borderStyle = UITextBorderStyleRoundedRect;  
  38.     [self.view addSubview:_textField];  
  39.       
  40.       
  41.     /** 
  42.      *  1.创建一个UIButton, 
  43.      *  2.并添加响应事件,从首页跳转到第二个页面. 
  44.      */  
  45.     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(1001205050) title:@"Push" target:self action:@selector(didClickButtonAction)];  
  46.     [self.view addSubview:button];  
  47.       
  48.       
  49.     // Do any additional setup after loading the view.  
  50. }  
  51.   
  52. - (void)didClickButtonAction  
  53. {  
  54.       
  55.     /** 
  56.      *  1.用push的方法推出下一个页面 
  57.      *  2.把首页输入框输入的字符串,通过SecondViewController类的属性NSString * text接收 
  58.      *  3.从而实现把首页输入框输入的字符串,传到第二页的UILabel上. 
  59.      */  
  60.     SecondViewController * secondVC = [[SecondViewController alloc]init];  
  61.     secondVC.text = _textField.text;  
  62.     [self.navigationController pushViewController:secondVC animated:YES];  
  63.     [secondVC release];  
  64. }  
  65.   
  66.   
  67. - (void)didReceiveMemoryWarning  
  68. {  
  69.     [super didReceiveMemoryWarning];  
  70.     // Dispose of any resources that can be recreated.  
  71. }  
  72.   
  73. @end  



[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #import "SecondViewController.h"  
  2.   
  3. @interface SecondViewController ()  
  4.   
  5. @end  
  6. @implementation SecondViewController  
  7.   
  8. - (void)dealloc  
  9. {  
  10.     [_label release];  
  11.     [super dealloc];  
  12. }  
  13. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  14. {  
  15.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  16.     if (self) {  
  17.         // Custom initialization  
  18.     }  
  19.     return self;  
  20. }  
  21.   
  22. - (void)viewDidLoad  
  23. {  
  24.     [super viewDidLoad];  
  25.     self.view.backgroundColor = [UIColor orangeColor];  
  26.     self.navigationItem.title = @"第二页";  
  27.     /** 
  28.      *  1.在第二个界面创建一个UILabel 
  29.      *  2.把首页输入框输入的字符串,通过SecondViewController类的属性NSString * text接收 
  30.      *  3.然后通过赋值给UILabel 
  31.      */  
  32.     _label = [[UILabel alloc]initWithFrame:CGRectMake(508020030)];  
  33.     _label.backgroundColor = [UIColor greenColor];  
  34.     _label.text = self.text;  
  35.     [self.view addSubview:_label];  
  36.       
  37.     // Do any additional setup after loading the view.  
  38. }  
  39.   
  40. - (void)didReceiveMemoryWarning  
  41. {  
  42.     [super didReceiveMemoryWarning];  
  43.     // Dispose of any resources that can be recreated.  
  44. }  




通知传值

//流程:
1.
注册通知
2.
通知中心,发送一条消息通知----------其中name名字千万不要写错了,会出现在3个地方
3.
实现通知中心内部的方法,并实现传值
4.
第四步,消息发送完,要移除掉

代码如下:

[objc] view plaincopy
  1. #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.     //第四步,消息发送完,要移除掉  
  15.     [[NSNotificationCenter defaultCenter]removeObserver:self name:@"labelTextNotification" object:nil];  
  16.     [super dealloc];  
  17. }  
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  19. {  
  20.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  21.     if (self) {  
  22.         // Custom initialization  
  23.     }  
  24.     return self;  
  25. }  
  26.   
  27. - (void)viewDidLoad  
  28. {  
  29.     [super viewDidLoad];  
  30.       
  31.       
  32.     self.view.backgroundColor = [UIColor redColor];  
  33.     self.navigationItem.title = @"首页";  
  34.       
  35.     _label = [[UILabel alloc]initWithFrame:CGRectMake(508020030)];  
  36.     _label.backgroundColor = [UIColor greenColor];  
  37.     [self.view addSubview:_label];  
  38.       
  39.       
  40.       
  41.     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(1001205050) title:@"Push" target:self action:@selector(didClickButtonAction)];  
  42.     [self.view addSubview:button];  
  43.       
  44.     //第二步,通知中心,发送一条消息通知----------其中name名字千万不要写错了,会出现在3个地方  
  45.     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showLabelText:) name:@"labelTextNotification" object:nil];  
  46.       
  47.    
  48.       
  49.       
  50.     // Do any additional setup after loading the view.  
  51. }  
  52.   
  53. - (void)showLabelText:(NSNotification *)notification  
  54. {  
  55.     //第三,实现通知中心内部的方法,并实现传值  
  56.     id text = notification.object;  
  57.     _label.text = text;  
  58. }  
  59.   
  60. - (void)didClickButtonAction  
  61. {  
  62.       
  63.     
  64.     SecondViewController * secondVC = [[SecondViewController alloc]init];  
  65.     [self.navigationController pushViewController:secondVC animated:YES];  
  66.     [secondVC release];  
  67. }  
  68.   
  69. - (void)didClick:(NSString *)text  
  70. {  
  71.     _label.text = text;  
  72. }  
  73.   
  74. - (void)didReceiveMemoryWarning  
  75. {  
  76.     [super didReceiveMemoryWarning];  
  77.     // Dispose of any resources that can be recreated.  
  78. }  
  79.   
  80. @end  

[objc] view plaincopy
  1. #import "SecondViewController.h"  
  2. #import "FirstViewController.h"  
  3. #import "UIButton+Create.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.       
  32.       
  33.     _textField = [[UITextField alloc]initWithFrame:CGRectMake(508020030)];  
  34.     _textField.borderStyle = UITextBorderStyleRoundedRect;  
  35.     [self.view addSubview:_textField];  
  36.       
  37.       
  38.       
  39.       
  40.     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(1001205050) title:@"Back" target:self action:@selector(didClickButtonAction)];  
  41.     [self.view addSubview:button];  
  42.    
  43.       
  44.       
  45.       
  46.       
  47.     // Do any additional setup after loading the view.  
  48. }  
  49.   
  50. - (void)didClickButtonAction  
  51. {  
  52.       
  53.     //第一步注册通知  
  54.     [[NSNotificationCenter defaultCenter]postNotificationName:@"labelTextNotification" object:_textField.text];  
  55.     [self.navigationController popToRootViewControllerAnimated:YES];  
  56. }  
  57.   
  58.   
  59.   
  60. - (void)didReceiveMemoryWarning  
  61. {  
  62.     [super didReceiveMemoryWarning];  
  63.     // Dispose of any resources that can be recreated.  
  64. }  
  65.   
  66. @end  










0 0
原创粉丝点击