swift中代理的使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift中代理的使用相关的知识,希望对你有一定的参考价值。
下面以自定义的UITableViewCell的代理为例,记录一下swift中代理的使用
controller中的代码如
1 // 2 // ViewController.swift 3 // simpleDemo 4 // 5 // Created by liubo on 16/7/25. 6 // Copyright © 2016年 liubo. All rights reserved. 7 // 8 9 import UIKit 10 11 class ViewController: UIViewController, 12 UITableViewDataSource, 13 UITableViewDelegate, 14 DelegateCellDelegate{ 15 16 var tableView: UITableView? 17 18 //MARK: -- lifeCycle 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 22 self.navigationController?.navigationBar.barTintColor = UIColor.cyanColor() 23 self.view.backgroundColor = UIColor.whiteColor() 24 25 initTableView() 26 } 27 28 //MARK: -- UITableViewDataSource 29 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 30 return 10 31 } 32 33 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 34 let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(DelegateCell), forIndexPath: indexPath) as? DelegateCell 35 cell!.textLabel?.text = "\\(indexPath.row)" 36 cell!.delegate = self 37 return cell! 38 } 39 40 //MARK: -- DelegateCellDelegate 41 func giveLike(btn: UIButton) { 42 print(123) 43 } 44 45 //MARK: -- private 46 func initTableView() { 47 let tableView = UITableView(frame: CGRectMake(0,0,self.view.width,self.view.height)) 48 tableView.backgroundColor = UIColor.whiteColor() 49 tableView.dataSource = self 50 tableView.delegate = self 51 tableView .registerClass(DelegateCell.classForCoder(), forCellReuseIdentifier: NSStringFromClass(DelegateCell)) 52 self.tableView = tableView 53 self.view.addSubview(self.tableView!) 54 } 55 }
Cell中的代码
1 // 2 // DelegateCell.swift 3 // simpleDemo 4 // 5 // Created by liubo on 16/7/26. 6 // Copyright © 2016年 liubo. All rights reserved. 7 // 8 9 import UIKit 10 11 protocol DelegateCellDelegate :NSObjectProtocol { 12 13 func giveLike(btn:UIButton) 14 } 15 16 class DelegateCell: UITableViewCell { 17 var likeButton: UIButton? 18 weak var delegate: DelegateCellDelegate? 19 20 override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 21 super.init(style: .Default, reuseIdentifier: reuseIdentifier) 22 initSubViews() 23 } 24 25 func initSubViews() { 26 let like = UIButton(type: .Custom) 27 like.frame = CGRectMake(UIScreen.mainScreen().bounds.size.width - 40, 10, 30, 30) 28 like.backgroundColor = UIColor.orangeColor() 29 like.setTitle("赞", forState: .Normal) 30 like.addTarget(self, action: Selector("lickClicked:"), forControlEvents: .TouchUpInside) 31 likeButton = like 32 self.addSubview(likeButton!) 33 } 34 35 func lickClicked(sender: UIButton) { 36 delegate?.giveLike(sender) 37 38 } 39 40 required init?(coder aDecoder: NSCoder) { 41 fatalError("init(coder) has not been implemented") 42 } 43 44 }
在代码中用到了UIView的extension,值得一提的是在swift中可以直接对一个对象的结构体属性的成员进行直接赋值,这一点在OC中是做不到的
如要给一个view设置y值,在swift中可以这样写
public var x: CGFloat {
get {
return self.frame.origin.x
}
set {
self.frame.origin.x = newValue
}
}
而在OC中则要这样写
- (void) setTop: (CGFloat) newtop {
CGRect newframe = self.frame;
newframe.origin.y = newtop;
self.frame = newframe;
}
extension的代码如下
1 // 2 // UIViewExtension.swift 3 // joke 4 // 5 // Created by liubo on 16/7/26. 6 // Copyright © 2016年 liubo. All rights reserved. 7 // 8 9 import Foundation 10 import UIKit 11 12 extension UIView { 13 //x--left 14 public var x: CGFloat { 15 get { 16 return self.frame.origin.x 17 } 18 set { 19 self.frame.origin.x = newValue 20 } 21 } 22 //y--top 23 public var y: CGFloat { 24 get { 25 return self.frame.origin.y 26 } 27 set { 28 self.frame.origin.y = newValue 29 } 30 } 31 //width 32 public var width: CGFloat { 33 get { 34 return self.frame.size.width 35 } 36 set { 37 self.frame.size.width = newValue 38 } 39 } 40 //height 41 public var height: CGFloat { 42 get { 43 return self.frame.size.height 44 } 45 set { 46 self.frame.size.height = newValue 47 } 48 } 49 //right 50 public var right: CGFloat { 51 get { 52 return self.frame.origin.x + self.frame.size.width 53 } 54 set { 55 self.frame.origin.x = newValue - self.frame.size.width 56 } 57 } 58 //bottom 59 public var bottom: CGFloat { 60 get { 61 return self.frame.origin.y + self.frame.size.height 62 } 63 set { 64 self.frame.origin.y = newValue - self.frame.size.height 65 } 66 } 67 68 //中心x 69 public var centerX :CGFloat { 70 get { 71 return self.center.x 72 } 73 set { 74 self.center.x = newValue 75 } 76 } 77 78 //中心y 79 public var centerY :CGFloat { 80 get { 81 return self.center.y 82 } 83 set { 84 self.center.y = newValue 85 } 86 } 87 88 public var orign: CGPoint { 89 get { 90 return self.frame.origin 91 } 92 set { 93 self.frame.origin = newValue 94 } 95 } 96 97 public var size: CGSize { 98 get { 99 return self.frame.size 100 } 101 set { 102 self.frame.size = newValue 103 } 104 } 105 }
在打出protocol DelegateCellDelegate {
func giveLike(btn:UIButton)
}后,发现不能用weak了修饰,代理我们都知道 arc中需要使用weak来避免循环引用造成内存泄漏,此时要在后面加上 :NSObjectProtocol,这样就能用weak来修饰
地址:http://pan.baidu.com/s/1dFbnCMP
以上是关于swift中代理的使用的主要内容,如果未能解决你的问题,请参考以下文章
Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题