swift创建代理
来源:互联网 发布:主机与设备传送数据时 编辑:程序博客网 时间:2024/05/17 23:44
最近稍微写了下swift,顺便努力的写了个代理的传值,贴出代码大家参考一下,最简单的方法写代理
基本流程是第二个页面的代理在第一个页面实现
第一个controller
import UIKit
class ViewController: UIViewController ,swiftDelegate{ //这里不要忘记引用代理
override func viewDidLoad() {
super.viewDidLoad()
let button =UIButton(frame: buttonrect)
button.backgroundColor = UIColor.redColor()
button.layer.cornerRadius =5
button.setTitle("button", forState:UIControlState.Normal)
button.addTarget(self, action:"buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func buttonClick(){ //button的跳转方法
print("aa")
let secondController = second()
secondController.delegate = self
self.navigationController!.pushViewController(secondController, animated:true)
}
func changeValue(controller: second, string: String) {//代理方法实现
print("\(string)")
}
创建第二个controllerimport UIKit
protocol swiftDelegate:NSObjectProtocol{
func changeValue(controller:second,string:String)
}
class second: UIViewController {
var delegate:swiftDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor =UIColor.whiteColor()
// Do any additional setup after loading the view.
let rect = CGRect(x: 100, y: 100, width: 100, height: 30)
let mybutton = UIButton(frame: rect)
mybutton .addTarget(self, action:"myButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
mybutton.backgroundColor = UIColor.redColor()
self.view.addSubview(mybutton)
}
func myButtonClick(){
print("点击")
if ((delegate) !=nil){
delegate?.changeValue(self, string:"传值" )
}
}
如果文档有疑问也可以下载demo https://github.com/yongchangye/demo
- swift创建代理
- swift-代理
- Swift-代理-delegate
- swift代理传值
- swift 中使用代理
- swift 委托/代理模式
- swift的代理
- swift实现代理
- Swift开发-代理
- Swift---代理和闭包传值
- iOS swift - 代理
- swift代理传值
- swift 代理简单实现
- swift-代理方法
- 【Swift】代理使用举例
- Swift 中的代理 (Delegate)
- Swift委托代理实现
- Swift - 创建代理协议实现页面间参数传递和方法调用
- 【codechef】n个数,多少种取法的异或值==m【二项式定理】
- 子元素浮动 父元素自适应高度
- java二进制,运算符算法说明
- 价值170万美元的Flash漏洞 360Vulcan免费提交给Adobe修复
- RDLC 报表 表达式
- swift创建代理
- perfect-scrollbar 和 fastclick 整合后首次需要点击两次的处理
- [Medical Image Processing] 3.1 Binary Morphology-Dilation(膨胀)&Erosion(腐蚀)
- 10_14.c打印signal mask 的函数。
- pthread条件变量condition(配合mutex锁使用)
- 防止重复提交。
- Jenkins构建Android项目持续集成之系统配置篇
- 缓存文件可以放在哪里?它们各自的特点是什么?
- 用二叉链表存储结构构造一棵二叉树,然后用栈结构进行非递归遍历