Swift 3.0 中swift protocol 可选中怎么添加可选方法

iOS_Swift初识之使用三种回调方式自定义Button - 小邱哥 - 博客园
Blog Stats
Stories - 0
Comments - 23
Trackbacks - 0
最近在学习Swift ,发现青玉伏案大神早期用OC写的一篇博客--& 很适合用来熟悉Swift的回调方式,于是我就用Swift翻版了一下,具体实现原理就不多说了,主要贴上Swift的代码给大家看看。由于刚开始了解Swift,有使用不恰当的地方请轻拍。。。。
1、新建一个xib,拖一个UIView到界面上,绑定上自定义的UIView类,拖一个UILabel到view上,写上Button假装是个UIButton;并将label拖到代码当中
2、我这里回调三种Button点击事件,TouchDown、TouchUpInside、TouchUpOutside
A、首先是Target:
a、首先声明一个枚举来设定点击类型
enum MyControlEvents{
case TouchUpInside
case TouchUpOutside
case TouchDown
b、设置Target、action和Event三个属性
//声明三个属性、添加一个addTarget方法,注意Target和delegate一样要用weak修饰
weak var target:AnyObject?
var action:Selector?
var controlEvents:MyControlEvents?
func addTarget(target:AnyObject!, action: Selector!, forMyControlEvents controlEvents: MyControlEvents! ){
self.target = target
self.action = action
self.controlEvents = controlEvents
&c、在touch事件的代理里面实现Target方法、并把label的颜色改改,这样才像button,我把代理方法写在了extension延展里面,因为我见苹果都这样
extension MyViewButton{
override func touchesBegan(touches: Set&UITouch&, withEvent event: UIEvent?) {
self.titleLabel.textColor = UIColor.lightGrayColor()
if self.controlEvents == MyControlEvents.TouchDown{
self.target?.performSelector(self.action!, withObject: self)
override func touchesEnded(touches: Set&UITouch&, withEvent event: UIEvent?) {
self.titleLabel.textColor = UIColor.blueColor()
//let point:CGPoint = (touches as NSSet).anyObject()!.locationInView(self)、下面方法的合体
let view = (touches as NSSet).anyObject()
let point:CGPoint = view!.locationInView(self)
//判断Target类型和触摸点移出情况相匹配时执行target方法
if CGRectContainsPoint(self.bounds, point) && self.controlEvents == MyControlEvents.TouchUpInside{
self.target?.performSelector(self.action!, withObject: self)
}else if !CGRectContainsPoint(self.bounds, point) && self.controlEvents == MyControlEvents.TouchUpOutside{
self.target?.performSelector(self.action!, withObject: self)
&d、在VC中实现,选择不同的点击类型即可监控不同的点击事件啦
class ViewController: UIViewController , MyViewButtonDelegate {
var myButton:MyViewButton?
override func viewDidLoad() {
super.viewDidLoad()
//从xib中加载我们自定义的view,我的xib叫做&View&
let bundel:NSBundle = NSBundle.mainBundle()
let views:Array = bundel.loadNibNamed("View", owner: nil, options: nil)
self.myButton = views.last as? MyViewButton
self.myButton?.frame = CGRectMake(80, 200, 200, 100)
self.view.addSubview(self.myButton!)
self.myButton?.addTarget(self, action: Selector!("didTapButton:"), forMyControlEvents: MyControlEvents.TouchUpInside)
最后&实现点击方法即可
func didTapButton(button:MyViewButton){
print("VC点击了按钮---点击类型是\(button.controlEvents)")
1、声明一个protocol,里面有三个可选实现的方法,并把自身当做参数带出去
objc protocol MyViewButtonDelegate:NSObjectProtocol{
optional func didTouchMyButton(button:MyViewButton)
optional func didTouchUpInsideButton(button:MyViewButton)
optional func didTouchUpOutsideButton(button:MyViewButton)
&2、声明一个delegate属性,同样是弱指针引用
weak var delegate:MyViewButtonDelegate!
&3、同样在touch事件中实现
extension MyViewButton{
//调用协议方法时判断一下delegate和协议方法是否存在
override func touchesBegan(touches: Set&UITouch&, withEvent event: UIEvent?) {
self.titleLabel.textColor = UIColor.lightGrayColor()
if self.delegate != nil && self.delegate!.respondsToSelector("didTouchMyButton:"){
self.delegate?.didTouchMyButton!(self)
override func touchesEnded(touches: Set&UITouch&, withEvent event: UIEvent?) {
self.titleLabel.textColor = UIColor.blueColor()
//let point:CGPoint = (touches as NSSet).anyObject()!.locationInView(self)
let view = (touches as NSSet).anyObject()
let point:CGPoint = view!.locationInView(self)
if CGRectContainsPoint(self.bounds, point){
if self.delegate != nil && self.delegate!.respondsToSelector("didTouchUpInsideButton:"){
self.delegate?.didTouchUpInsideButton!(self)
if self.delegate != nil && self.delegate!.respondsToSelector("didTouchUpOutsideButton:"){
self.delegate?.didTouchUpOutsideButton!(self)
&4、在VC中实现即可
class ViewController: UIViewController , MyViewButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let bundel:NSBundle = NSBundle.mainBundle()
let views:Array = bundel.loadNibNamed("View", owner: nil, options: nil)
self.myButton = views.last as? MyViewButton
self.myButton?.frame = CGRectMake(80, 200, 200, 100)
self.view.addSubview(self.myButton!)
//设置button的代理
self.myButton!.delegate = self
extension ViewController{
//实现代理方法
func didTouchMyButton(button: MyViewButton) {
print("delegate--VC点击了button")
func didTouchUpInsideButton(button: MyViewButton) {
print("delegate--TouchUpInside")
func didTouchUpOutsideButton(button: MyViewButton) {
print("delegate--TouchUpOutside")
&C、闭包(block)
1、首先在自定义view里实现,相当于typedef一个block类型
typealias MyBlock = (button:MyViewButton)-&Void
&2、声明三个block属性,并且声明三个给block赋值方法
var TouchBlockHandel:MyBlock?
var TouchUpInsideBlockHandel:MyBlock?
var TouchUpOutsideBlockHandel:MyBlock?
//也可以不写方法直接属性赋值
func setMyTouchBlock(block:MyBlock){
self.TouchBlockHandel = block
func setMyTouchUpInsideBlock(block:MyBlock){
self.TouchUpInsideBlockHandel = block
func setMyTouchUpOutsideBlock(block:MyBlock){
self.TouchUpOutsideBlockHandel = block
&3、在touch事件中实现block
extension MyViewButton{
override func touchesBegan(touches: Set&UITouch&, withEvent event: UIEvent?) {
self.titleLabel.textColor = UIColor.lightGrayColor()
self.TouchBlockHandel!(button: self)
override func touchesEnded(touches: Set&UITouch&, withEvent event: UIEvent?) {
self.titleLabel.textColor = UIColor.blueColor()
//let point:CGPoint = (touches as NSSet).anyObject()!.locationInView(self)
let view = (touches as NSSet).anyObject()
let point:CGPoint = view!.locationInView(self)
if CGRectContainsPoint(self.bounds, point){
self.TouchUpInsideBlockHandel!(button: self)
self.TouchUpOutsideBlockHandel!(button: self)
&4、同样在VC中给三个block赋值即可
class ViewController: UIViewController , MyViewButtonDelegate {
var myButton:MyViewButton?
override func viewDidLoad() {
super.viewDidLoad()
let bundel:NSBundle = NSBundle.mainBundle()
let views:Array = bundel.loadNibNamed("View", owner: nil, options: nil)
self.myButton = views.last as? MyViewButton
self.myButton?.frame = CGRectMake(80, 200, 200, 100)
self.view.addSubview(self.myButton!)
self.myButton?.setMyTouchBlock({ (button:MyViewButton) -& Void in
print("block--VC点击了button")
self.myButton?.setMyTouchUpInsideBlock({ (button:MyViewButton) -& Void in
print("block--VCTouchUpInside")
self.myButton?.setMyTouchUpOutsideBlock({ (button:MyViewButton) -& Void in
print("block--VCTouchUpOutside")
&最后来看看三个方法写在一起的打印结果。就添加了一个target监控TouchUpInside。总体来说和OC逻辑没有任何变化,只是语法上有所不同,block还是好用
&好了,这样就结束了,今天下雪了,大家注意保暖!推荐这篇日记的豆列
&&&&&&&&&&&&主题 : Swift普通类如何继承Protocol?(含答案)
级别: 新手上路
UID: 243044
可可豆: 105 CB
威望: 69 点
在线时间: 211(时)
发自: Web Page
来源于&&分类
Swift普通类如何继承Protocol?(含答案)&&&
比如在OC中是:
@interface CustomDataSource : NSObject &UITableViewDataSource&
但是在Swift中有错误提示:
class CellDatasDataSource : UITableViewDataSource {
    
提示信息:1.Type 'CellDatasDataSource' does not conform to protocol 'NSObjectProtocol'2.Type 'CellDatasDataSource' does not conform to protocol 'UITableViewDataSource'正确的写法应该是这样:
class CellDatasDataSource : NSObject, UITableViewDataSource {
    &&&& // 注意,这里实现UITableViewDataSource的这两个方法不用加 override 关键字
&&&&&&&& func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -& Int {
&&&&&&&&&&&&&&&&&& // TODO...
&&&&&&&& }
&&&&&&&& func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -& UITableViewCell! {
&&&&&&&&&&&&&&&&&&// TODO...
&&&&&&&& }
[ 此帖被jace.fu在 09:16重新编辑 ]
级别: 风云使者
UID: 83747
发帖: 3864
可可豆: 19533 CB
威望: 19404 点
在线时间: 2395(时)
发自: Web Page
你的CellDatasDataSource是继承自NSObject么?如果是,实现这两个协议必须的方法就行了。
级别: 新手上路
可可豆: 40 CB
威望: 30 点
在线时间: 43(时)
发自: Web Page
必须实现&&&& func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -& Int&&&&&&&&&&&&func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -& UITableViewCell!这两个方法
级别: 新手上路
UID: 243044
可可豆: 105 CB
威望: 69 点
在线时间: 211(时)
发自: Web Page
回 1楼(chenxin) 的帖子
级别: 新手上路
UID: 243044
可可豆: 105 CB
威望: 69 点
在线时间: 211(时)
发自: Web Page
回 2楼(小米6s) 的帖子
非常感谢! &&&&&&
级别: 新手上路
可可豆: 2 CB
威望: 2 点
在线时间: 17(时)
发自: Web Page
回 1楼(chenxin) 的帖子
classFirstViewController: UIViewController,UIAlertViewDelegate,UITableViewDelegate, UITableViewDataSource, ZBarReaderViewDelegate,UIImagePickerControllerDelegate, AVCaptureMetadataOutputObjectsDelegate{//中间省略func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -& Int    {        return 0    }     func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -& UITableViewCell!{letCellID:NSString= &adBusinessTableViewCell&        var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(CellID) as UITableViewCell         return cell    }}一样提示 Type 'FirstViewController' does not conform to protocol 'UITableViewDataSource' 请问如何解决
级别: 新手上路
UID: 83540
可可豆: 181 CB
威望: 122 点
在线时间: 45(时)
发自: Web Page
级别: 新手上路
可可豆: 17 CB
威望: 8 点
在线时间: 340(时)
发自: Web Page
感觉这个不应该叫 类 继承协议, 应该叫 遵循 协议, 叫继承听起来怪怪的。 协议继承协议或者类继承类听起来才对味、class CellDatasDataSource : UITableViewDataSource {&&&&}这种写法,明显是类的继承,是类与类之间的。 而UITableViewDataSource是个协议,所以出错。@interface CustomDataSource : NSObject &UITableViewDataSource&
@end而这种写法很明显就是类遵循协议,而不是继承协议。 两个都不一样,怎么比?明显从oc 翻译到 swift 就出问题了
级别: 新手上路
可可豆: 16 CB
威望: 16 点
在线时间: 27(时)
发自: Web Page
去掉感叹号!!!!
关注本帖(如果有新回复会站内信通知您)
苹果公司现任CEO是谁?2字 正确答案:库克
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版}

我要回帖

更多关于 dubbo protocol 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信