第一个视图控制器:

import UIKit

// 遵循协议

class ViewController: UIViewController,SecondVCDelegate

{

override func viewDidLoad() {

super.viewDidLoad()

// 创建一个button

let button1 = UIButton(frame: CGRectMake(120, 120, 50, 50))

// 修改背景颜色

button1.backgroundColor = UIColor.redColor()

self.view.addSubview(button1)

// 点击方法

button1.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

}

// button点击方法

func buttonAction(sender:UIButton){

// 跳转下一个界面

let secondVC = SecondViewController()

// 属性传值

secondVC.passValue = "咻"

// 指定代理

// secondVC.delegate = self

// 定义block

secondVC.block = { (tempColor:UIColor)->Void in

self.view.backgroundColor = tempColor

}

self.navigationController?.pushViewController(secondVC, animated: true)

}

// 实现代理方法

func changeColor(tempColor: UIColor) {

self.view.backgroundColor = tempColor

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

}

}

/*************************************************************/

代理的六大步骤

1.后页面制定协议

2.前页面代理遵循协议

3.后页面设置代理属性

4.前页面指定代理self

5.后页面发送代理方法命令

6.前页面代理实现代理方法

第二个视图控制器:

import UIKit

protocol SecondVCDelegate {

// 协议中的方法

func changeColor(tempColor:UIColor)

}

class SecondViewController: UIViewController {

// 属性

var passValue:String?

// 代理属性

var delegate:SecondVCDelegate?

// block属性

var block:((UIColor)->Void)?

override func viewDidLoad() {

super.viewDidLoad()

self.view.backgroundColor = UIColor.cyanColor()

print(self.passValue!)

self.title = self.passValue

// 返回button

let button2 = UIButton(frame: CGRectMake(120, 120, 50, 50))

button2.backgroundColor = UIColor.blackColor()

button2.addTarget(self, action: "button2Action:", forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(button2)

}

// button2点击方法

func button2Action(sender:UIButton) {

// 发送代理方法命令

// self.delegate?.changeColor(UIColor.redColor())

// 调用block

self.block!(UIColor.redColor())

// 返回

self.navigationController?.popViewControllerAnimated(true)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

Swift代理和传值的更多相关文章

  1. swift中的传值

    光阴似箭,日月如梭,转眼间学习的旅途已经过了一大半了,忘着自己所敲过的成批的代码,看着自己付出和努力,默默地为自己这几个月的奋斗感到欣慰,不论学习的路途再怎么的艰辛,但是自己还是坚持过来了,回想着以往 ...

  2. OC10_代理反向传值

    // // ProtectedDelegate.h // OC10_代理反向传值 // // Created by zhangxueming on 15/6/24. // Copyright (c) ...

  3. OC9_代理正向传值

    // // ProtectedDeleagate.h // OC9_代理正向传值 // // Created by zhangxueming on 15/6/24. // Copyright (c) ...

  4. Swift代理的使用

    Swift代理的使用 协议规定了用来实现某一特定功能所必需的方法和属性. 任意能够满足协议要求的类型被称为遵循(conform)这个协议. 类,结构体或枚举类型都可以遵循协议,并提供具体实现来完成协议 ...

  5. Swift: 比较Swift中闭包传值、OC中的Block传值

    一.介绍 开发者对匿名函数应该很清楚,其实它就是一个没有名字的函数或者方法,给人直观的感觉就是只能看到参数和返回值.在iOS开发中中,它又有自己的称呼,在OC中叫Block代码块,在Swift中叫闭包 ...

  6. Swift 08.页面传值

    OC中经常用到的一种初始化控制器的方法.比如A push B.并且将A的一个值:value 传递给B 使用.简单的办法就是 在B重构init方法.比如在.h 中定义: - ()initValue:(N ...

  7. iOS 代理反向传值

    在上篇博客 iOS代理协议 中,侧重解析了委托代理协议的概念等,本文将侧重于它们在开发中的应用. 假如我们有一个需求如下:界面A上面有一个button.一个label.从界面A跳转到界面B,在界面B的 ...

  8. iOS-设计模式之代理反向传值

    代理设计模式就是自己的方法自己不实现,让代理对象去实现. 可以让多个类实现一组方法. 委托模式的好处在于: 1.避免子类化带来的过多的子类以及子类与父类的耦合 2.通过委托传递消息机制实现分层解耦 代 ...

  9. Swift代理造成内存泄漏的解决办法

    在swift中,使用代理 ,可能很多人会这样实现: .首先定义一份协议. protocol ToolProrocol{ //代理方法 func didRecieveResults(result:Int ...

随机推荐

  1. Tomcat发布项目,域名访问

    域名访问项目 1,去掉访问路径的端口号: 找到 Tomcat 下的 conf 文件中的 server.xml,找到 8080 修改成 80, 2,项目绑定域名: <Host name=" ...

  2. [Codeforces]Codeforces Round #489 (Div. 2)

    Nastya and an Array 输出有几种不同的数字 #pragma comment(linker, "/STACK:102400000,102400000") #ifnd ...

  3. Android开发笔记(5)——方法调用(基础)

    转载请注明——博客园igoslly:http://www.cnblogs.com/igoslly/p/6833544.html   在实际方法调用中,程序按顺序逐句执行,直到“}”结束. 为避免程序大 ...

  4. JS——旋转木马

    1.opacity和zIndex的综合运用 2.样式的数组的替换:向右边滑动---删除样式数组第一位并在数组最后添加:向左边滑动---删除样式数组最后一位并在数组前添加 3.开闭原则,只有当回调函数执 ...

  5. JS——轮播图简单版

    1.小图标版本 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  6. windows远程桌面连接

    服务器端: 1.我的电脑->管理->本地用户和组->用户->新建用户设置账号密码,隶属于administrator和remote user 2.我的电脑->属性-> ...

  7. 12--C++_运算符重载

    C++_运算符重载 什么是运算符的重载? 运算符与类结合,产生新的含义. 为什么要引入运算符重载? 作用:为了实现类的多态性(多态是指一个函数名有多种含义) 怎么实现运算符的重载? 方式:类的成员函数 ...

  8. codeforces_731D_(前缀和)(树状数组)

    D. 80-th Level Archeology time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  9. php省市区三级联动

    效果 步骤 前端:通过ajax请求获取数据,使用了jquery 页面一开始加载所有省份信息 ->当选择省下拉框后触发改变监听时间-change ->当选择市下拉框后触发改变监听时间-cha ...

  10. Uedior上传大文件超时报错

    出错原因: 1.php超时等待时间太短 2.uedior中设置了请求超时,提示信息: 上传失败,请重试 先解决第一个问题: 设置php.ini中的max_execution_time 为0 (意思是h ...