Objective-C基础笔记(5)Protocol
2023-09-14 09:10:23 时间
Protocol简单来说就是一系列方法的列表,当中声明的方法能够被不论什么类实现。这中模式一般称为代理(delegation)模式。
在IOS和OS X开发中,Apple採用了大量的代理模式来实现MVC中View(UI控件)和Controller(控制器)的解耦。
以下我们先来看一下我们熟悉的Android中的button监听过程,然后再对照理解delegation。
首先我建立一个非常easy的Androidproject,在Layout中放置一个button,例如以下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/mybutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/> </LinearLayout>
package com.example.helloword; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.mybutton); button.setOnClickListener(new MyOnClickListener()); } class MyOnClickListener implements OnClickListener{ @Override public void onClick(View arg0) { Toast.makeText(MainActivity.this, "点击了button", Toast.LENGTH_SHORT).show(); } } class MyonLongClickListener implements OnLongClickListener{ @Override public boolean onLongClick(View arg0) { Toast.makeText(MainActivity.this, "长按了button", Toast.LENGTH_SHORT).show(); return false; } } }OnClickListener是View的一个内部类,是View定义的一个接口,我们打开OnClickListener源代码例如以下:
/** * Interface definition for a callback to be invoked when a view is clicked. */ public interface OnClickListener { /** * Called when a view has been clicked. * * @param v The view that was clicked. */ void onClick(View v); }我们再来看看setOnClickListener方法
public void setOnClickListener(OnClickListener l) { if (!isClickable()) { setClickable(true); } getListenerInfo().mOnClickListener = l; }先推断View是不是可点击的,主要我们来看看以下那一句,getListenerInfo().mOnClickListener = 1;
ListenerInfo getListenerInfo() { if (mListenerInfo != null) { return mListenerInfo; } mListenerInfo = new ListenerInfo(); return mListenerInfo; }从这段代码能够看出来,将我们的OnClickListener实例保存到了ListenerInfor对象中了,那么ListenerInfor对象是用来干嘛的呢?因为我当下没有Android系统源代码就不在跟踪下去了,能够猜想这个类持有我们的OnClickeListener对象,当系统响应屏幕点击事件的时候,通过事件分发,能够调用onClick方法来告诉全部实现了OnClickeListener接口的对象。
接下来我们来模拟一下IOS中button监听的实现。
Button.h文件
#import <Foundation/Foundation.h> @class Button; //<>代表实现某个协议 //这里相当于OnClickListener @protocol ButtonDelegate <NSObject> //将Button对象传递给监听器,来推断详细的调用实例 - (void) onClick:(Button *)btn; @end @protocol ButtonLongClickDelegate <NSObject> - (void) onLongClick:(Button *)btn; @end @interface Button : NSObject //delegate就是button的监听器 //id代表不论什么OC对象 @property (nonatomic, retain) id<ButtonDelegate> delegate; @property (nonatomic, retain) id<ButtonLongClickDelegate> longClickDeleate; //模拟系统调用click方法 - (void)click; //模拟系统调用longclick方法 - (void)longClick; @endButton.m文件
#import "Button.h" @implementation Button - (void)click { //button被点击了,就应该通知监听器(这里是模拟) //假设onClick方法被实现,调用onClick方法 if([_delegate respondsToSelector:@selector(onClick:)]){ [_delegate onClick:self]; }else{ NSLog(@"onClick监听器未实现"); } } - (void)longClick { //button被长按(模拟系统) if([_delegate respondsToSelector:@selector(onClick:)]){ [_longClickDeleate onLongClick:self]; }else{ NSLog(@"longClick监听器未实现"); } } - (void)dealloc { [_delegate release]; [_longClickDeleate release]; [super dealloc]; } @endButtonListener.h
#import <Foundation/Foundation.h> @protocol ButtonDelegate; //实现button点击协议 @interface ButtonListener : NSObject <ButtonDelegate> @endButtonListener.m
#import "ButtonListener.h" #import "Button.h" @implementation ButtonListener - (void)onClick:(Button *)btn { NSLog(@"button被点击了"); } @endButtonLongClickListener.h文件
#import <Foundation/Foundation.h> //对协议进行提前声明,跟@class的用途是一样的 @protocol ButtonLongClickDelegate; @interface ButtonLongClickListener : NSObject <ButtonLongClickDelegate> @endButtonLongClickListener.m文件
#import "ButtonLongClickListener.h" #import "Button.h" @implementation ButtonLongClickListener - (void)onLongClick:(Button *)btn{ NSLog(@"button被长按了"); } @endmain.m文件
#import <Foundation/Foundation.h> #import "Button.h" #import "ButtonListener.h" #import "ButtonLongClickListener.h" int main(int argc, const char * argv[]) { @autoreleasepool { //初始化一个button Button *button = [[[Button alloc] init] autorelease]; //初始化一个button的监听器 ButtonListener *listener = [[[ButtonListener alloc] init] autorelease]; //初始化一个button长按监听器 ButtonLongClickListener *longClickListener = [[[ButtonLongClickListener alloc] init] autorelease]; //设置button的监听器 button.delegate = listener; //设置长按button监听器 button.longClickDeleate = longClickListener; //点击button [button click]; //长按button [button longClick]; } return 0; }输出结果:
2014-11-16 13:52:35.215 ProtocalTest[845:82273] button被点击了
2014-11-16 13:52:35.216 ProtocalTest[845:82273] button被长按了
相关文章
- Coursera台大机器学习基础课程学习笔记1 -- 机器学习定义及PLA算法
- 程序员找工作必备 PHP 基础面试题
- Win10系列:C#应用控件基础8
- C#基础视频教程1 背景知识和安装配置
- python基础学习笔记(七)
- Java实现 蓝桥杯VIP 基础练习 完美的代价
- Java实现 蓝桥杯VIP 基础练习 报时助手
- Java实现 蓝桥杯 基础练习 特殊的数字
- Java基础——集合源码解析 List List 接口
- spring cloud 学习笔记 基础工程的构建(一)
- Dart基础-类
- 笔记6:Django基础
- 【原创】开源Math.NET基础数学类库使用(17)C#计算矩阵条件数
- 100天精通Python(爬虫篇)——第47天:selenium自动化操作浏览器(基础+代码实战)
- 野生前端的数据结构基础练习(3)——链表
- 字节跳动最爱考的web前端面试题:计算机网络基础
- 0基础转行学软件测试,哪些技术是必须要掌握的?
- 0基础学习软件测试,如何快速的入门?
- python基础===requests学习笔记
- PyTorch学习笔记(四):PyTorch基础实战
- 大数据开发笔记(六):Flume基础学习
- Java学习笔记基础(中)
- 信号完整性(SI)电源完整性(PI)学习笔记(四)电阻的物理基础
- 【Java 基础】类和对象 & 方法重载详解
- Web前端开发JavaScript基础
- JSP — 知识总结篇《I》【基础语法】
- 【微服务笔记20】微服务组件之Nacos配置中心基础环境搭建、配置持久化、动态刷新配置
- pytorch学习笔记(九):卷积神经网络CNN(基础篇)
- Linux基础笔记12 | I/O重定向之输出、输入