zoukankan      html  css  js  c++  java
  • protocol的简单写法

     1 //
     2 //  TouchView.h
     3 //  LessonUIControlAndSubClass
     4 
     5 
     6 
     7 #import <UIKit/UIKit.h>
     8 
     9 @class TouchView;
    10 //1.制定协议,协议名字格式:类名+Delegate
    11 @protocol TouchViewDelegate <NSObject>
    12 
    13 @optional
    14 - (void)touchBegan:(TouchView *)touchView;
    15 - (void)touchMoved:(TouchView *)touchView;
    16 - (void)touchEnded:(TouchView *)touchView;
    17 - (void)touchCancelled:(TouchView *)touchView;
    18 
    19 @end
    20 
    21 @interface TouchView : UIView
    22 
    23 //2.写属性,属性名delegate,类型是id,并且要遵守协议<类名Delegate>
    24 @property (assign, nonatomic) id<TouchViewDelegate> delegate;
    25 
    26 @end

     1 //
     2 //  TouchView.m
     3 //  LessonUIControlAndSubClass
     4 //
     5 
     6 #import "TouchView.h"
     7 
     8 @implementation TouchView
     9 
    10 //3.一旦找到代理,让代理执行事情
    11 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    12     //判断delegate是否实现了某方法
    13     if ([_delegate respondsToSelector:@selector(touchBegan:)]) {
    14         [_delegate touchBegan:self];
    15     }
    16 }
    17 
    18 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    19     if ([_delegate respondsToSelector:@selector(touchMoved:)]) {
    20         [_delegate touchMoved:self];
    21     }
    22 }
    23 
    24 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    25     if ([_delegate respondsToSelector:@selector(touchEnded:)]) {
    26         [_delegate touchEnded:self];
    27     }
    28 }
    29 
    30 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    31     if ([_delegate respondsToSelector:@selector(touchCancelled:)]) {
    32         [_delegate touchCancelled:self];
    33     }
    34 }
    35 
    36 @end
  • 相关阅读:
    使用window.showModalDialog的一个问题
    ASP.NET 的<system.codedom>标签
    SqlServer事务中的可重复读和序列化隔离界别
    C#使用163的SMTP服务器发送邮件
    在HTML中小心使用空格和回车
    ASP.NET 4.0的ClientIDMode属性
    C/C++中判断某一文件或目录是否存在<转>
    新公司工作有感
    网络编程(转)<下>
    VC控件ListControl的使用
  • 原文地址:https://www.cnblogs.com/jiangdaohong/p/4596873.html
Copyright © 2011-2022 走看看