2011-04-14 6 views
7

हाय मैं आईफोन में नया हूं, क्या कोई मुझे बता सकता है। प्रतिनिधि (उपयोगकर्ता परिभाषित) बनाने के लिए, मैंने इस पर एक शोध किया है, लेकिन किसी भी संतोषजनक जवाब खोजने के लिए सक्षम नहीं, किसी भी मदद अत्यधिक सराहना की जाएगी धन्यवादअपना खुद का डेलगेट कैसे बनाएं (उद्देश्य सी में उपयोगकर्ता परिभाषित प्रतिनिधि)

उत्तर

17

सबसे पहले इस तरह एक प्रतिनिधि एक घोषणा को परिभाषित -

@interface IconDownloader : NSObject 
{ 
    NSIndexPath *indexPathInTableView; 
    id <IconDownloaderDelegate> delegate; 
    NSMutableData *activeDownload; 
    NSURLConnection *imageConnection; 
} 
- तो फिर इस तरह एक प्रतिनिधि वस्तु बनाने

@protocol IconDownloaderDelegate; 

इसके लिए एक संपत्ति घोषित करें -

@property (nonatomic, assign) id <IconDownloaderDelegate> delegate; 

यह परिभाषित करें -

@protocol IconDownloaderDelegate 

- (void)appImageDidLoad:(NSIndexPath *)indexPath; 

@end 

तो फिर तुम इस प्रतिनिधि पर तरीकों कॉल कर सकते हैं -

[delegate appImageDidLoad:self.indexPathInTableView]; 

यहाँ की पूरी स्रोत कोड है छवि डाउनलोडर वर्ग -

.h फ़ाइल -

@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 

मीटर फ़ाइल -

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

मीटर फ़ाइल में - -

#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 

और यहाँ उसका उपयोग कैसे करते है

- (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; 
    } 
} 
+0

धन्यवाद सौरभ। –

1

यह बुनियादी अवधारणाओं एक स्वयं के प्रतिनिधि

प्रतिनिधियों मैन्युअल रूप से ऐप्स में दृश्य नियंत्रकों की सरणी के भीतर स्थानांतरण नियंत्रित करने के लिए बहुत उपयोगी होते हैं बनाने के लिए है। प्रतिनिधियों का उपयोग करके आप नियंत्रण प्रवाह को बहुत अच्छी तरह से प्रबंधित कर सकते हैं।

यहाँ खुद प्रतिनिधियों की छोटा सा उदाहरण है ....

  1. एक प्रोटोकॉल कक्षा बनाएं .... (ज केवल)

SampleDelegate.h

#import 


@protocol SampleDelegate 
@optional 

#pragma Home Delegate 

-(NSString *)getViewName; 

@end 
  1. कक्षा में प्रोटोकॉल कक्षा के ऊपर आयात करें जिसे आप किसी अन्य वर्ग के प्रतिनिधि बनाना चाहते हैं। यहाँ मेरे पूर्व में। मैं HomeViewController ऑब्जेक्ट के प्रतिनिधि बनाने के लिए AppDelegate का उपयोग कर रहा हूं।

भी प्रतिनिधि संदर्भ <>

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]; 
} 
संबंधित मुद्दे