嗨 我是新來的iPhone,可以讓任何人知道如何創建委託(用戶定義),我已經做了這方面的研究,但無法找到任何滿意的答覆,任何幫助,將不勝感激 謝謝如何創建我自己的delgate(用戶定義的委託在目標c)
7
A
回答
17
首先定義一個聲明一個代理這樣的 -
@protocol IconDownloaderDelegate;
然後創建一個委託對象這樣的 -
@interface IconDownloader : NSObject
{
NSIndexPath *indexPathInTableView;
id <IconDownloaderDelegate> delegate;
NSMutableData *activeDownload;
NSURLConnection *imageConnection;
}
聲明一個屬性爲它 -
@property (nonatomic, assign) id <IconDownloaderDelegate> delegate;
定義它 -
@protocol IconDownloaderDelegate
- (void)appImageDidLoad:(NSIndexPath *)indexPath;
@end
然後就可以調用這個委託方法 -
[delegate appImageDidLoad:self.indexPathInTableView];
這裏的完整的源代碼圖片下載類 -
.h file -
@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文件 -
#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
,這裏是我們如何使用它 -
#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文件 -
- (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];
}
}
這裏是代表獲得自動調用 -
// 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;
}
}
1
這是基本概念來創建自己的委託
委託是手動控制的應用程序視圖控制器的陣列內轉移非常有用的。使用委託可以很好地管理控制流。
這裏是自己的代表小例子....
- 創建一個協議類....(.H只)
SampleDelegate.h
#import
@protocol SampleDelegate
@optional
#pragma Home Delegate
-(NSString *)getViewName;
@end
- 導入上面的協議類,你想讓另一個類的委託的類。在我的前身。我使用AppDelegate來使HomeViewController的對象的委託。
也代表參考<>
ownDelegateAppDelegate添加上述DelegateName。ħ
#import "SampleDelegate.h"
@interface ownDelegateAppDelegate : NSObject <UIApplicationDelegate, SampleDelegate> {
}
ownDelegateAppDelegate.m
//setDelegate of the HomeViewController's object as
[homeViewControllerObject setDelegate:self];
//add this delegate method definition
-(NSString *)getViewName
{
return @"Delegate Called";
}
HomeViewController.h
#import
#import "SampleDelegate.h"
@interface HomeViewController : UIViewController {
id<SampleDelegate>delegate;
}
@property(readwrite , assign) id<SampleDelegate>delegate;
@end
HomeViewController.h
- (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];
}
相關問題
- 1. 如何創建的UITextField的自定義委託目標C
- 2. 在目標中創建自定義協議和委託的相對優勢-c
- 3. 通知目標C中的自定義委託類C
- 4. 如何在目標C中創建委託人?
- 5. 如何創建委託給ngModel的自定義AngularJS指令?
- 6. 設置委託給自己的委託?
- 7. 如何在iOS 8中創建自定義委託?
- 8. C++委託創建
- 9. 如何爲UIView子類創建自定義委託協議?
- 10. 自己的委託方法
- 11. 創建我自己的自定義標記(JSF 2.0)的問題
- 12. 使用AFNetworking 2.0創建自己的委託和協議
- 13. 如何建立我們自己的用戶定義
- 14. 如何創建我自己的Jaxb2RootElementHttpMessageConverter自定義版本?
- 15. 如何創建我自己的自定義命令
- 16. 如何創建我自己的自定義按鈕?
- 17. 如何創建我自己的自定義地圖
- 18. 從目標c類調用swift類的自定義委託引發錯誤
- 19. 如何使用佔位符目標c創建自定義UITextView?
- 20. SerialPort C#自定義DataReceived委託
- 21. 在C#中爲我自己的項目創建我自己的插件
- 22. 委託與參數定義但用作參數沒有自己的參數? c#
- 23. 有可能在AppDelegate中創建一個自定義委託?
- 24. 如何用自己的控件創建自定義的GTK#Widget?
- 25. 創建我自己的用戶實體
- 26. 在obj-c中創建非自我委託
- 27. jQuery:委託給自己?
- 28. 我如何創建我自己的自定義功能Closure模板在Plovr
- 29. 如何在mediawiki中創建自己的自定義組?
- 30. 創建一個對象作爲委託 - 目標C
由於SAURABH。 – 2011-04-14 11:03:26