簡體   English   中英

Objective-c中的自定義委托示例

[英]Custom Delegate Example in Objective-c

我了解在與其他對象(例如UITextField等)相關聯的情況下使用委托的方法,但是我試圖找到一個簡單的示例來獨立設置/使用自定義委托。 任何幫助/指針將不勝感激。

到目前為止,我的理解是:

#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:

我在一個簡單的基本應用程序中遇到的問題是在哪里實例化它,如何明智地對其進行內存管理以及如何調用它/獲取委托以提供一些簡單的反饋。 使我煩惱的最初概念之一是,協議中定義的方法標頭是在符合協議的對象上實現的。

加里

委派只是一種設計模式,幾乎沒有涉及任何儀式。 通常,如果您嘗試委托一個方法,則要檢查委托對象是否實現了所討論的方法(響應選擇器),如果是,則將調用傳遞給該對象。

這是Appel關於該主題的文檔的一個很好的起點。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM