How to create my own delgate (user defined delegate in objective c)
本问题已经有最佳答案,请猛点这里访问。
您好!我刚接触过iPhone,有人能告诉我吗?如何创建一个代表(用户定义),我对此做了一个研究,但没有找到满意的答案,任何帮助都会非常感谢。谢谢您
首先定义这样的声明委托-
1 | @protocol IconDownloaderDelegate; |
然后创建这样的委托对象-
1 2 3 4 5 6 7 | @interface IconDownloader : NSObject { NSIndexPath *indexPathInTableView; id <IconDownloaderDelegate> delegate; NSMutableData *activeDownload; NSURLConnection *imageConnection; } |
为其声明属性-
1 | @property (nonatomic, assign) id <IconDownloaderDelegate> delegate; |
定义它-
1 2 3 4 5 | @protocol IconDownloaderDelegate - (void)appImageDidLoad:(NSIndexPath *)indexPath; @end |
然后可以调用此委托的方法-
1 | [delegate appImageDidLoad:self.indexPathInTableView]; |
这是图像下载类的完整源代码-
h文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @class AppRecord; @class RootViewController; @protocol IconDownloaderDelegate; @interface IconDownloader : NSObject { AppRecord *appRecord; NSIndexPath *indexPathInTableView; id <IconDownloaderDelegate> delegate; NSMutableData *activeDownload; NSURLConnection *imageConnection; } @property (nonatomic, retain) AppRecord *appRecord; @property (nonatomic, retain) NSIndexPath *indexPathInTableView; @property (nonatomic, assign) id <IconDownloaderDelegate> delegate; @property (nonatomic, retain) NSMutableData *activeDownload; @property (nonatomic, retain) NSURLConnection *imageConnection; - (void)startDownload; - (void)cancelDownload; @end @protocol IconDownloaderDelegate - (void)appImageDidLoad:(NSIndexPath *)indexPath; @end |
m文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #import"IconDownloader.h" #import"MixtapeInfo.h" #define kAppIconHeight 48 #define TMP NSTemporaryDirectory() @implementation IconDownloader @synthesize appRecord; @synthesize indexPathInTableView; @synthesize delegate; @synthesize activeDownload; @synthesize imageConnection; #pragma mark - (void)dealloc { [appRecord release]; [indexPathInTableView release]; [activeDownload release]; [imageConnection cancel]; [imageConnection release]; [super dealloc]; } - (void)startDownload { self.activeDownload = [NSMutableData data]; // alloc+init and start an NSURLConnection; release on completion/failure NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:appRecord.mixtape_image]] delegate:self]; self.imageConnection = conn; [conn release]; } - (void)cancelDownload { [self.imageConnection cancel]; self.imageConnection = nil; self.activeDownload = nil; } #pragma mark - #pragma mark Download support (NSURLConnectionDelegate) - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.activeDownload appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { // Clear the activeDownload property to allow later attempts self.activeDownload = nil; // Release the connection now that it's finished self.imageConnection = nil; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // Set appIcon and clear temporary data/image UIImage *image = [[UIImage alloc] initWithData:self.activeDownload]; self.appRecord.mixtape_image_obj = image; self.activeDownload = nil; [image release]; // Release the connection now that it's finished self.imageConnection = nil; // call our delegate and tell it that our icon is ready for display [delegate appImageDidLoad:self.indexPathInTableView]; } @end |
下面是我们如何使用它-
1 2 3 4 5 6 7 | #import"IconDownloader.h" @interface RootViewController : UITableViewController <UIScrollViewDelegate, IconDownloaderDelegate> { NSArray *entries; // the main data model for our UITableView NSMutableDictionary *imageDownloadsInProgress; // the set of IconDownloader objects for each app } |
在.m文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | - (void)startIconDownload:(AppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath { IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath]; if (iconDownloader == nil) { iconDownloader = [[IconDownloader alloc] init]; iconDownloader.appRecord = appRecord; iconDownloader.indexPathInTableView = indexPath; iconDownloader.delegate = self; [imageDownloadsInProgress setObject:iconDownloader forKey:indexPath]; [iconDownloader startDownload]; [iconDownloader release]; } } |
这里是自动调用的委托-
1 2 3 4 5 6 7 8 9 10 11 12 | // called by our ImageDownloader when an icon is ready to be displayed - (void)appImageDidLoad:(NSIndexPath *)indexPath { IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath]; if (iconDownloader != nil) { UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:iconDownloader.indexPathInTableView]; // Display the newly loaded image cell.imageView.image = iconDownloader.appRecord.appIcon; } } |
这是创建自己的委托的基本概念
代理对于在应用程序中手动控制视图控制器数组内的传输非常有用。使用委托可以很好地管理控制流。
下面是自己的代表的一个小例子……
采样elegate.h
1 2 3 4 5 6 7 8 9 10 11 | #import @protocol SampleDelegate @optional #pragma Home Delegate -(NSString *)getViewName; @end |
同时在委托引用中添加上面的DelegateName<>
owndelegateappdelegate.h(W.ndelegateappdelegate.h)
1 2 3 4 5 | #import"SampleDelegate.h" @interface ownDelegateAppDelegate : NSObject <UIApplicationDelegate, SampleDelegate> { } |
owndelegateappdelegate.m
1 2 3 4 5 6 7 8 | //setDelegate of the HomeViewController's object as [homeViewControllerObject setDelegate:self]; //add this delegate method definition -(NSString *)getViewName { return @"Delegate Called"; } |
家庭视图控制器.h
1 2 3 4 5 6 7 8 9 10 11 | #import #import"SampleDelegate.h" @interface HomeViewController : UIViewController { id<SampleDelegate>delegate; } @property(readwrite , assign) id<SampleDelegate>delegate; @end |
家庭视图控制器.h
1 2 3 4 5 6 7 8 9 10 | - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; UILabel *lblTitle = [[UILabel alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; lblTitle.text = [delegate getViewName]; lblTitle.textAlignment = UITextAlignmentCenter; [self.view addSubview:lblTitle]; } |