ios中几种传值方式

1 篇文章 0 订阅

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 plain copy
  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 plain copy
  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 plain copy
  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传值

代码如下:

  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  


 

 
 
  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. {  



属性/方法传值

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

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

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

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

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

代码如下:

[objc]  view plain copy 在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 plain copy 在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 plain copy
  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 plain copy
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
iOS,可以通过WKWebView与JavaScript进行交互传值。以下是一些常见的方法: 1. 使用JavaScriptCore框架:在iOS 7及以上版本,可以使用JavaScriptCore框架来实现WKWebView与JS的交互传值。具体实现步骤如下: (1)在iOS创建一个JSContext实例,通过该实例可以操作JS环境。 (2)将JSContext实例设置到WKWebView的配置对象。 (3)在JS调用iOS方法时,可以通过window.webkit.messageHandlers对象进行调用,例如window.webkit.messageHandlers.methodName.postMessage(params)。 (4)在iOS,可以通过WKScriptMessageHandler代理方法来接收JS传递的消息,并进行处理。 2. 使用WKScriptMessageHandler协议:在iOS 8及以上版本,可以通过WKScriptMessageHandler协议来实现WKWebView与JS的交互传值。具体实现步骤如下: (1)在WKWebView的配置对象设置WKUserContentController的代理对象,并添加需要监听的JS方法名。 (2)在JS调用iOS方法时,可以通过window.webkit.messageHandlers对象进行调用,例如window.webkit.messageHandlers.methodName.postMessage(params)。 (3)在iOS,可以通过WKScriptMessageHandler代理方法来接收JS传递的消息,并进行处理。 无论使用哪种方法,都需要注意JS和iOS之间传递的数据类型和格式。通常,JS传递的是字符串类型的数据,在iOS需要进行解析和转换。同时,为了避免安全问题,需要对JS传递的数据进行校验和过滤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值