zoukankan      html  css  js  c++  java
  • delegate的练习

    //在RootViewController.h里面遵守协议

    #import <UIKit/UIKit.h>

    @interface RootViewController : UIViewController

    @end

     -----------------------------------<>-------------------------

    //RootViewController.m

    #import "RootViewController.h"

    #import "TouchView.h"

    @interface RootViewController ()<TouchViewDelegate>{

        TouchView *touch;

    }

    @end

    @implementation RootViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        touch = [[TouchView alloc] initWithFrame:CGRectMake(10, 65, 100, 100)];

        touch.backgroundColor = [UIColor redColor];

        touch.delegate = self;

        [self.view addSubview:touch];

        [touch release];

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    #pragma mark - TouchViewDelegate

    -(void)canTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

        touch.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.0];

    }

    @end

     ----------------------------<自己定义的TouchView, 继承于UIView>-----------------------------

    #import <UIKit/UIKit.h>

    @protocol TouchViewDelegate <NSObject>//制订协议, 协议名 : 类名 + delegate

    -(void)canTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;//制定协议里面的方法

    @end

    @interface TouchView : UIView

    @property (nonatomic, retain) id<TouchViewDelegate>delegate;

    @end

     ----------------------------------------------<>-------------------------------------------

    #import "TouchView.h"

    @implementation TouchView

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    //让代理执行本应该TouchView完成的方法

        if ([_delegate respondsToSelector:@selector(canTouchesEnded:withEvent:)]) {

            [_delegate canTouchesEnded:touches withEvent:event];//把参数传进来

        }

        

        

    //    self.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.0];

    }

    @end

  • 相关阅读:
    红外图像非均匀性校正相关汇总
    【matlab】图像去噪的代码测试
    基于字典学习的图像超分辨率相关
    基于FP-Growth算法的关联性分析——学习笔记
    Hadoop:相关概念
    Sybase:删除表中的某列
    Sybase:循环调用存储过程
    Python3.x:定时自动发送邮件
    问题:如何对两个文件夹中的文件进行对比
    Sybase数据库:两个特别注意的地方
  • 原文地址:https://www.cnblogs.com/hsxblog/p/4926160.html
Copyright © 2011-2022 走看看