我想了解委託模式是如何工作的。下面是我嘗試過的一些代碼,但問題是代理方法downloadStarted和downloadFinished從不調用。委託模式
我在這裏想念什麼?
DownloaderDelegate.h
@protocol DownloaderDelegate <NSObject>
-(void)downloadStarted;
-(void)downloadFinished;
@end
Downloader.h
#import "DownloaderDelegate.h"
@interface Downloader : NSObject
@property (nonatomic, retain) id<DownloaderDelegate>delegate;
-(void)fileIsDownloaded;
-(void)downloadFile;
@end
Downloader.m
@implementation Downloader
@synthesize delegate = _delegate;
-(void)downloadFile
{
[[self delegate] downloadStarted];
[NSTimer timerWithTimeInterval:5
target:self
selector:@selector(fileIsDownloaded)
userInfo:nil
repeats:NO];
}
-(void)fileIsDownloaded
{
[[self delegate]downloadFinished];
}
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
....
Downloader *d = [[Downloader alloc]init];
[d downloadFile];
[d release];
....
}
-(void)downloadStarted
{
NSLog(@"Started");
}
-(void)downloadFinished
{
NSLog(@"Finished");
}
閱讀本 - [代表和數據源(https://developer.apple.com/ library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#// apple_ref/doc/uid/TP40002974-CH7-SW18) – beryllium 2012-02-22 08:33:37