Objective-c中的自定义代理示例 [英] Custom Delegate Example in Objective-c

查看:76
本文介绍了Objective-c中的自定义代理示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解使用与其他对象(即UITextField等)相关联的代理,但我试图找到一个独立设置/使用自定义代理的简单示例。非常感谢任何帮助/指针。

I understand using delegates in associated with other objects (i.e. UITextField etc.) But I am trying to find a simple example of setting up / using a custom delegate independently. Any help / pointers would be much appreciated.

我的理解是:

#import <Foundation/Foundation.h>

@class TheBox;

@protocol TheBoxDelegate <NSObject>
-(void)theBoxDelegateWillDoSomething:(TheBox *)theBox;
@end

@interface TheBox : NSObject {
    id <TheBoxDelegate> delegate;
}
@property(assign) id <TheBoxDelegate> delegate;
@end

#import "TheBox.h"

@implementation TheBox
@synthesize delegate;

@end

// Some other class will conform to <TheBoxDelegate> and 
// implement the method -(void)theBoxDelegateWillDoSomething:

在一个简单的基本应用程序中是实例化这个,如何管理它记忆明智和如何调用它/获取委托给一些简单的反馈。引发我的最初概念之一是协议中定义的方法头在符合协议的对象上实现。

The problem I am having within a simple basic app is where to instantiate this, how to manage it memory wise and how to call it / get the delegate to give some simple feedback. One of the initial concepts that was tripping me up was that the method header defined in the protocol is implemented on the object conforming to the protocol.

Gary。

推荐答案

授权只是一个设计模式,涉及的仪式很少。一般来说,如果你试图委托一个方法,你检查委托对象是否实现了所讨论的方法(响应选择器),如果是这样,你将调用传递给该对象。

Delegation is just a design pattern, there is very little ceremony involved. Generally if you are trying to delegate a method you check to see if the delegate object implements the method in question (responds to the selector) and if so you pass the invocation on to that object.

这是一个很好的起点,在Appel的文档的主题。

Here is a good starting point in Appel's Docs on the subject.

http://developer.apple.com/mac/library/documentation/cocoa/conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html

这篇关于Objective-c中的自定义代理示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆