当前位置:首页 > CN2资讯 > 正文内容

代理传值代理传值原理

2天前CN2资讯

代理(delegate)传值 ---- 顾名思义就是委托别人办事,就是当一件事情发生后,自己不处理,让别人来处理。

//代理传值
注意:从后往前传(如果从前往后传,会传不成功)
//流程:
1.后一个界面定义一个协议,并且定义一个属性叫delegate
2.在前一个界面进入后一个界面的瞬间,(即:创建完成一个界面之后),让前一个界面作为后一个界面的delegate
3.前一个界面实现代理方法
4.后一个界面在合适的机会, 让代理, 执行,代理方法 (传的值以参数的形式 含在代理方法里)

代码如下:

[objc] view plaincopy

  • #import "FirstViewController.h"  

  • #import "SecondViewController.h"  

  • #import "UIButton+Create.h"  

  • @interface FirstViewController ()  

  • {  

  •     UILabel * _label;  

  • }  

  • @end  

  •   

  • @implementation FirstViewController  

  • - (void)dealloc  

  • {  

  •     [_label release];  

  •     [super dealloc];  

  • }  

  • - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  

  • {  

  •     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  

  •     if (self) {  

  •         // Custom initialization  

  •     }  

  •     return self;  

  • }  

  •   

  • - (void)viewDidLoad  

  • {  

  •     [super viewDidLoad];  

  •     self.view.backgroundColor = [UIColor redColor];  

  •     self.navigationItem.title = @"首页";  

  •     self.view.userInteractionEnabled = YES;  

  •     /** 

  •      *  1.创建一个UIButton, 

  •      *  2.并添加响应事件,从首页跳转到第二个页面. 

  •      */  

  •     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)];  

  •     button.userInteractionEnabled = YES;  

  •     [self.view addSubview:button];  

  •       

  •       

  •       

  •     /** 

  •      *  1.在第1个界面创建一个UILabel 

  •      *  2.把第二页输入框输入的字符串,通过代理方法传过来 

  •      *  3.然后通过赋值给UILabel 

  •      */  

  •     _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];  

  •     _label.backgroundColor = [UIColor greenColor];  

  •     [self.view addSubview:_label];  

  •       

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

  • }  

  •   

  • - (void)didClickButtonAction  

  • {  

  •       

  •     /** 

  •      *  1.用push的方法推出下一个页面 

  •      *  2.把第二页输入框输入的字符串,通过代理方法传过来 

  •      *  3.从而实现把首页输入框输入的字符串,传到第二页的UILabel上. 

  •      */  

  •     SecondViewController * secondVC = [[SecondViewController alloc]init];  

  •     secondVC.delegate = self;//确认代理  

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

  •     [secondVC release];  

  • }  

  •   

  • //实现代理方法  

  • - (void)passByValueSecondVC:(SecondViewController *)secondVC text:(NSString *)text  

  • {  

  •     _label.text = text;  

  • }  

  •   

  •   

  • - (void)didReceiveMemoryWarning  

  • {  

  •     [super didReceiveMemoryWarning];  

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

  • }  

  •   

  • @end  


  • [objc] view plaincopy

  • #import <UIKit/UIKit.h>  

  • @class SecondViewController;  

  • @protocol secondViewControllerDelegate <NSObject>  

  •   

  • - (void)passByValueSecondVC:(SecondViewController *)secondVC text:(NSString *)text;  

  •   

  • @end  

  •   

  • @interface SecondViewController : UIViewController  

  • @property (nonatomic,assign)id<secondViewControllerDelegate>delegate;  

  • @end  



  • [objc] view plaincopy

  • #import "SecondViewController.h"  

  • #import "UIButton+Create.h"  

  • #import "FirstViewController.h"  

  • @interface SecondViewController ()  

  • {  

  •     UITextField * _textField;//创建一个输入框  

  • }  

  • @end  

  • @implementation SecondViewController  

  •   

  • - (void)dealloc  

  • {  

  •     [_textField release];  

  •     [super dealloc];  

  • }  

  • - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  

  • {  

  •     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  

  •     if (self) {  

  •         // Custom initialization  

  •     }  

  •     return self;  

  • }  

  •   

  • - (void)viewDidLoad  

  • {  

  •     [super viewDidLoad];  

  •     self.view.backgroundColor = [UIColor orangeColor];  

  •     self.navigationItem.title = @"第二页";  

  •     /** 

  •      *  1.创建一个UIButton, 

  •      *  2.并添加响应事件,从第二个页面返回到首页. 

  •      */  

  •     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Back" target:self action:@selector(didClickButtonAction)];  

  •     [self.view addSubview:button];  

  •       

  •     /** 

  •      *  1.在第二个界面创建一个输入框 

  •      * 

  •      */  

  •     _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];  

  •     _textField.borderStyle = UITextBorderStyleRoundedRect;  

  •     [self.view addSubview:_textField];  

  •       

  •       

  •       

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

  • }  

  •   

  • - (void)didClickButtonAction  

  • {  

  •     //调用代理方法,_textField.text传值  

  •     [_delegate passByValueSecondVC:self text:_textField.text];  

  •     [self.navigationController popToRootViewControllerAnimated:YES];  

  • }  

  •   

  • - (void)didReceiveMemoryWarning  

  • {  

  •     [super didReceiveMemoryWarning];  

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

  • }  

  •   

  • @end  




    • 你可能想看:

      扫描二维码推送至手机访问。

      版权声明:本文由皇冠云发布,如需转载请注明出处。

      本文链接:https://www.idchg.com/info/24622.html

      标签: 代理传值
      分享给朋友:

      “代理传值代理传值原理” 的相关文章

      企业云计算的首选:Oracle Cloud VPS详解及其优势

      在现代企业日益依赖云计算的背景下,Oracle Cloud VPS(虚拟专用服务器)因其强大的功能和灵活的解决方案,成为了很多企业的热门选择。简单来说,Oracle Cloud VPS通过虚拟化技术将物理服务器划分成多个独立的虚拟服务器,为用户提供了一种安全、可靠且高效的云计算体验。在这里,我将为大...

      详细指南:如何进行Linux扩容以解决存储不足问题

      什么是Linux扩容? 在使用Linux操作系统时,随着数据的增加,我们常常面临磁盘空间不足的问题。Linux扩容就是通过添加新的磁盘、扩展现有磁盘容量或利用逻辑卷管理(LVM)等方式,来增加系统的存储空间。扩容可以帮助我更好地管理数据,提高服务器的运行效率。 我记得第一次接触扩容时,面对不断增加的...

      Vultr Cloud Computer与High Frequency服务器的全面对比

      在云服务的世界中,Vultr无疑是一个备受瞩目的名字。它提供两种主要的云服务器类型:Vultr Cloud Compute和High Frequency服务器。这两种服务器各有其独特之处,适合不同类型的用户和使用场景。 首先,Vultr Cloud Compute是其最基础的产品,主要以高性价比为卖...

      强制结束占用短裤:高效解决文件锁定问题的方法与工具

      强制结束占用短裤这一概念听起来可能有些陌生,但在计算机操作系统中,它扮演着一个非常重要的角色。当一个文件或进程被占用时,我们常常会发现自己无法删除、移动或修改这些文件。这时,强制结束的必要性就显而易见了。通过强制结束占用,我们可以有效地解除阻碍,重新获得对文件的掌控。 对于普通用户来说,主动解除文件...

      Digital-VM评测:性价比高的VPS主机服务与全球数据中心优势分析

      Digital-VM是一家在2018年成立于美国的主机商,专注于提供VPS主机服务。自成立以来,Digital-VM致力于为全球用户提供高效、低价的云计算解决方案。随着虚拟主机市场的快速发展,Digital-VM不断扩展其服务和产品线,逐渐成长为一家具备竞争力的主机商。在技术方面,Digital-V...

      Vultr DD Windows安装教程:轻松一步到位

      在云计算越来越流行的今天,Vultr作为一个强大的云服务提供商,吸引了大量用户。对于想在Vultr服务器上安装Windows的用户来说,使用DD命令是一种非常便利的方法。接下来,我将为你详细介绍如何通过这一方式在Vultr上安装Windows。 1.1 使用DD命令直接安装Windows 1.1.1...