这个代理传值是经常使用的一种传值方式,下面介绍一种View 和 Controller 之间的代理传值方法。

先建立一个View视图

如 LoginView 是继承于一个UIView

在LoginView.h里面声明协议

LoginView.h文件

   #import <UIKit/UIKit.h>

@class LoginView;

//1.声明协议

@protocol LoginViewDelegate

@optional//可选的

  • (void)sureButtonTaped:(LoginView *)loginView info:(NSString *)info;

    @end

    @interface LoginView : UIView

    //2.声明delegate属性

    @property (nonatomic,assign) id delegate;

    @end

在LoginView.m 有一个textField,一个button,点击button,将textField里面的值传入Controller里面。

LoginView.m文件

import "LoginView.h"

@interface LoginView ()

@property (nonatomic,strong)UITextField *textField;

@property (nonatomic,strong) UIButton *button;

@end

@implementation LoginView

  • (instancetype)initWithFrame:(CGRect)frame

    {

    self = [super initWithFrame:frame];

    if (self) {

    self.backgroundColor = [UIColor yellowColor];

      [self setUp];

    }

    return self;

    }

  • (void)setUp{

    _textField = [[UITextField alloc] init];

    _textField.bounds = CGRectMake(0, 0, CGRectGetWidth(self.bounds) * 0.7, 40);

    _textField.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds) - 100);

    _textField.tintColor = [UIColor redColor];

    _textField.borderStyle = UITextBorderStyleLine;

    _textField.keyboardType = UIKeyboardTypeASCIICapable;

    _textField.placeholder = @"请输入文字";

    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;

    [self addSubview:_textField];

    _button = [UIButton buttonWithType:UIButtonTypeSystem];

    _button.frame = CGRectMake(120, 280, 80, 30);

    [_button setTitle:@"登陆" forState:UIControlStateNormal];

    [_button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:_button];

}

  • (void)buttonTaped:(UIButton *)sender

    {

    //调用协议方法

    [_delegate sureButtonTaped:self info:_textField.text];

}

在这里我们用于接收的视图就用一开始的ViewController,你也可以传入你想要传入的视图

ViewController.h文件

import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

ViewController.m文件

#import "ViewController.h"

#import "LoginView.h"

//引入协议

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {

    [super viewDidLoad];

    LoginView *login = [[LoginView alloc]initWithFrame:CGRectMake(20, 200, 375-40, 350)];

    //1.设置代理

    login.delegate = self;

    [self.view addSubview:login];

}

#pragma mark -- LoginViewDelegate

//3.实现协议方法

  • (void)sureButtonTaped:(LoginView *)loginView info:(NSString *)info

    {

    NSLog(@"%@",info);

}

  • (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

@end

总结:代理加方法的传值是一种很好的传值方式,我们可以将自己要传入的值写进方法里面,打包传入,方便快捷。。

View 与 Controller 之间的delegate(代理)传值的更多相关文章

  1. 【MVC架构】——怎样利用Json在View和Controller之间传递数据

    在MVC架构中,尽管非常多东西和三层非常相似,可是也有非常大的差别.就比方传递数据.在三层架构中,传递数据就仅仅要一层返回,另外一层用同样类型的变量来接收即可了.在MVC中,事实上原理是一样的,Con ...

  2. 【MVC框架】——View和Controller之间的传值

    在MVC中,Controller运行一个能够说是路由功能.它通过View传过来的数据,来决定应该调用哪一个Model,相同会把Model处理完的数据传给View,所以就总是涉及到Controller和 ...

  3. 【ASP.NET MVC】View与Controller之间传递数据

    1   概述 本篇文章主要从操作上简要分析Controller<=>View之间相互传值,关于页面之间传值,如果感兴趣,可参考我另外一篇文章ASP.NET 页面之间传值的几种方式 . Co ...

  4. MVC进阶学习--View和Controller之间的数据传递(二)

    1. 使用Request.Form MVC 将页面简单化,与WebForm中的事件机制完全不同,就和普通的html标签表单提交没有任何区别(当然WebForm中的事件机制其实也是表单提交).在表单提交 ...

  5. spring mvc controller接收请求值及controller之间跳转及传值

    spring接收请求参数: 1,使用HttpServletRequest获取 @RequestMapping("/login.do") public String login(Ht ...

  6. MVC进阶学习--View和Controller之间的数据传递(一)

    1.使用ViewData ViewData 的是ControllerBase 的一个属性,是一个数据字典类型的,其实现代码如(这段代码来自asp.net MVC开源项目中源码)下: Code   1  ...

  7. AngularJS in Action读书笔记2——view和controller的那些事儿

    今天我们来818<angularjs in action>的第三章controller和view. 1.Big Picture概览图 View是angularjs编译html后呈现出来的, ...

  8. view向controller提交列表

    第一次将view中列表提交到controller,尝试了下,还是可以的 要传输的实体类 /// <summary> /// 用于展示的角色类 /// </summary> pu ...

  9. ASP.NET MVC轻教程 Step By Step 4——Model、View和Controller

    ASP.NET MVC中的Model(数据模型)主要包括定义数据结构.数据库读写.数据验证等等和对象处理相关的工作. 在解决方案资源管理器中找到Model文件夹,点击右键,添加一个新类,名为“Mess ...

随机推荐

  1. [BZOJ3223]Tyvj 1729 文艺平衡树

    [BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...

  2. js array push 添加内容

    向数组中天机内容: var array = new Array(); array.push('newItem');

  3. java中面向对象的一些知识(一)

    一:基本概念 举例:写一个程序,实现如下功能: 一群宠物,宠物有各种类型,如猫.狗.企鹅等 让这群宠物,按照各自的能力不同,进行各种比赛(如爬树.游泳.跳水):扩展性需求: 游泳比赛游泳池的参数.飞盘 ...

  4. C#接口的作用(经典)

    C#接口是一个让很多初学C#者容易迷糊的东西,用起来好像很简单,定义接口,里面包含方法,但没有方法具体实现的代码,然后在继承该接口的类里面要实现接口的所有方法的代码,但没有真正认识到接口的作用的时候就 ...

  5. 最常用的ES6特性(转)

    最常用的ES6特性 let, const, class, extends, super, arrow functions, template string, destructuring, defaul ...

  6. linux 文件系统

    / 根目录 /bin 存放着启动时所需要的普通程序.很多程序在启动以后也很有用,它们放在这个目录下是因为它们经常要被其他程序调用 /boot 很多Linux系统把内核映像和其他一些和启动有关的文件都放 ...

  7. cell单选

    先上图给看看效果 cell单选逻辑就是取出上一个选中的cell 设置图片为默认图片 在取出点击的cell 设置图片为选中图片即可 废话不多说直接上代码 p.p1 { margin: 0.0px 0.0 ...

  8. 远程CDN加速不可用,加载本地库

    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery ...

  9. webkit 模拟点击 winform

    刚在园子里看到有博主将WebBowser控件替换为Chrome内核(),链接http://www.cnblogs.com/gdyblog/p/WebKitBrowser.html 于是我想既然实现了替 ...

  10. CI中的数据库操作

    转载于:http://blog.sina.com.cn/s/blog_76e7bdba01016p2p.html CI中第一次连接数据库,在控制器或模型的构造函数里输入以下语句 $this->l ...