2

I have a protocol in one class:

@protocol DataStorageManager

- (void) saveFile;

@end

@interface DataManager : NSObject
{
    id <DataStorageManager> delegate;
}

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

//methods

@end

and its implementation:

@implementation DataManager

@synthesize delegate;

@end

and I have another class which is the adapter between the first and the third one:

#import "DataManager.h"
#import "DataPlistManager.h"

@interface DataAdapter : NSObject <DataStorageManager>

@property (nonatomic,strong) DataPlistManager *plistManager;
- (void) saveFile;

@end

and its implementation

#import "DataAdapter.h"

@implementation DataAdapter

-(id) initWithDataPlistManager:(DataPlistManager *) manager
{
    self = [super init];
    self.plistManager = manager;
    return self;
}

- (void) saveFile
{
    [self.plistManager savePlist];
}

@end

So when I in first method try to call my delegate method like this

[delegate saveFile]; 

Nothing happened. I don't understand what's wrong with the realization - it's a simple adapter pattern realization. So I need to use the delegate which will call the methods from the third class. Any help?

ShurupuS
  • 2,923
  • 2
  • 24
  • 44

2 Answers2

2

You are not setting the delegate property. You need to do this,

-(id) initWithDataPlistManager:(DataPlistManager *) manager
{
    self = [super init];
    self.plistManager = manager;
    self.plistManager.delegate = self;
    return self;
}

Also, in DataManager class remove the ivar declaration, just declaring property is sufficient, the ivar gets automatically created. Call the delegate method as below,

if([self.delegate respondsToSelector:@selector(saveFile)] {
    [self.delegate saveFile]; 
}

Hope that helps!

Amar
  • 13,202
  • 7
  • 53
  • 71
  • now there is an error _Property 'delegate' not found on object of type 'DataPlistManager *'_ What's wrong now? – ShurupuS Sep 05 '13 at 16:38
  • @ShurupuS Are you sure you want to pass `DataPlistManager` instance in `initWithDataPlistManager:` method or should it be `DataManager` instance instead? – Amar Sep 06 '13 at 06:49
  • What is `DataPlistManager` class anyways? Should it be subclass of `DataManager` class? – Amar Sep 06 '13 at 06:50
  • @Aamar DataPlistManager is just a class which contain some methods for work with plist. I try to use the adapter pattern - the main idea that I can easily change the class for working with different formats of files (DataPlistManager) without many changes in code. So in adapter I should call the methods from DataPlistManager. Sorry, I'm the new one in iOS dev and may be my description isn't complete. – ShurupuS Sep 06 '13 at 07:00
  • @ShurupuS So in the above code, you expect `DataPlistManager` to call the delegate method which is implemented by `DataAdapter`? If that is the case, I think `DataPlistManager` should be subclass of `DataManager`. – Amar Sep 06 '13 at 07:04
  • Ohhh... I totally confused with this case.. As I see `DataManager` will take somehow `DataStorageManager` which is a `DataAdapter` and `Data Adaptor` will use tghe present DataPlistManager class for all the next work with plist file.. That's how I would like to use all this construction. – ShurupuS Sep 06 '13 at 07:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36907/discussion-between-shurupus-and-amar) – ShurupuS Sep 06 '13 at 07:15
  • 1
    @ShurupuS Then in that case, in your `DataManager` class, you need to write something like this:: `self.delegate=adapterInstance`, this will make the `DataAdapter` instance the delegate, as required. Hope you get my point. – Amar Sep 06 '13 at 07:15
1

In your case you forget to set your protocol delegate and also need to call protocol method
by self.delegate....

I just Give Basic Idea for how to Create Protocol

Also Read This Question

#DetailViewController.h

#import <UIKit/UIKit.h>

@protocol MasterDelegate <NSObject>
-(void) getButtonTitile:(NSString *)btnTitle;
@end


@interface DetailViewController : MasterViewController

@property (nonatomic, assign) id<MasterDelegate> customDelegate; 

#DetailViewController.m

if([self.customDelegate respondsToSelector:@selector(getButtonTitile:)])
{
          [self.customDelegate getButtonTitile:button.currentTitle];    
}

#MasterViewController.m

create obj of DetailViewController

DetailViewController *obj = [[DetailViewController alloc] init];
obj.customDelegate = self;
[self.navigationController pushViewController:reportTypeVC animated:YES];

and add delegate method in MasterViewController.m for get button title.

#pragma mark -
#pragma mark - Custom Delegate  Method

-(void) getButtonTitile:(NSString *)btnTitle;
{
    NSLog(@"%@", btnTitle);

}
Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137