iOS delegation doesn't work - ios

I'm trying to implement delegate method between 2 classes in my app but the delegated method is not called.
Here is my code :
PBPartnersService.h
#protocol PBPartnersServicesDelegate <NSObject>
#required
-(void) didReceiveNewsDatasFromPartners:(NSDictionary *)Datas;
#end
Then I make my #property :
#interface PBPartnersServices : NSObject
#property (nonatomic, weak) id<PBPartnersServicesDelegate> delegate;
#end
In my PBPartnersService.m I call my delegate method (when I print self.delegate I get 'nil') :
if ([self.delegate respondsToSelector:#selector(didReceiveNewsDatasFromPartners:)]) {
[self.delegate didReceiveNewsDatasFromPartners:obj];
}
In my other class PBTicketsService.h I instantiate the first one :
#interface PBTicketsService : NSObject <PBPartnersServicesDelegate>
#property (nonatomic,strong) NSDictionary *ticketList;
#property (nonatomic, strong) PBPartnersServices *partnersServices;
- (void) prepareForDelegate;
#end
I made a method in PBTicketsService.m to set the partnersServices as a delegate :
- (void) prepareForDelegate{
self.partnersServices = [[PBPartnersServices alloc] init];
[self.partnersServices setDelegate:self];
}
and then I have my function who is never call :
-(void) didReceiveNewsDatasFromPartners:(NSDictionary *)Datas{
}

You should use Dependency injection: either add ticketsService as an argument of your partnerServices constructor, or do the opposite.

When you allocate the partnerServices property a second time your partnerServices will save in a new memory-adress. And for this object you didn't set the delegate property.

Related

Delegate method not called objective c

So I am calling delegate function but some how its not getting called, I tried everything from all the other similar threads but nothing works. It looks all good but the method is still not called. here is my code below -
So i created protocol like this -
AuthViewController.h
#class AuthViewController;
#protocol AuthViewControllerDelegate <NSObject>
- (void)updateNavigation:(NSString*)pageType
message:(NSString*)message;
#end
created a property -
#property (nonatomic, weak)id delegate;
And called the function in AuthViewController.m -
[self.delegate updateNavigation:#"xx" message:#"xx"];
Then in other class -
AssociateViewController.h
#interface AssociateViewController : UIViewController <AuthViewControllerDelegate>
#property (nonatomic, strong)AuthViewController *vc;
#End
AssociateViewController.m
First set the delegate in a button action or viewWillAppear(I tried both)-
self.vc = [[AuthViewController alloc] init];
self.vc.delegate = self;
And the here is the method which is somehow never called :( -
- (void)updateNavigation:(NSString*)pageType
message:(NSString*)message;
{
//method to do
}
Any help would be much appreciated. Thanks!

iOS: how class notify something to UI view?

Starting point
I have a class, say A, used by an UI view.
A has a delegate that should notify UI view and this one should be write something on screen.
Question
What is the best approach to achieve this feature?
Seems something like observer-observable pattern
Code
---A.h
#interface A : NSObject
#end
---A.m
#implementation A
-(void)fooDelegate:(FooType *)sender {
/* Here I need to notify UI (that change notificationArea.text) */
}
---UIView.h
#interface UIView : UIViewController
#property(strong, nonatomic, retain) A* a;
#property(strong, nonatomic) IBOutlet UITextField *notificationArea;
#end
Based on the comments, I guess just code is what you're looking for...
Create your delegate protocol:
#protocol ADelegate;
#interface A : NSObject
#property (nonatomic, weak) id <ADelegate> delegate;
#end
#protocol ADelegate <NSObject>
#optional
-(void)fooDelegate:(A *)a;
#end
Notify your delegate:
#implementation A
-(void)fooDelegate:(FooType *)sender {
if ([[self delegate] respondsToSelector:#selector(fooDelegate:)]) {
[[self delegate] fooDelegate:self];
}
}
#end
Conform to the delegate protocol:
#import "A.h"
#import "MyView.h"
#interface MyView <ADelegate>
#end
#implementation MyView
-(void)fooDelegate:(A *)a {
// update text field here
}
#end
Finally, whenever you create an instance of A, set the delegate (where self in this example is an instance of MyView:
A *a = [[A alloc] init];
[a setDelegate:self];

pass delegating object type as protocol method argument

I want to pass my delegating object as an argument with a declared type so I don't have to cast (if I pass (id)sender instead):
#protocol myObjectDelegate <NSObject>
- (void)myObjectAsArgument:(myObject *)object;
#end
#interface myObject : NSObject
// stuff...
#property (nonatomic, strong) id <myObjectDelegate> delegate;
#end
What would be the correct way of doing this? Like I said, I know I could do this:
- (void)myObjectAsArgument:(id)object;
And that would let me pass self in as argument, but I don't like using the cast syntax.
Thanks.
NOTE:
Apple does that too:
#protocol UITableViewDataSource <NSObject>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
#end
#interface UITableView : UIScrollView
#property (nonatomic, strong) id <UITableViewDataSource> dataSource;
#end
That's just one method from the UITableViewDataSource protocol and it's passing a UITableView type as argument. ;)
Since this is top Google search result, here's how to do it:
#class myObject; // just need this for compiling
#protocol myObjectDelegate <NSObject>
- (void)myObjectAsArgument:(myObject *)object;
#end
#interface myObject : NSObject
// stuff...
#property (nonatomic, strong) id <myObjectDelegate> delegate;
#end
You should do it this way:
- (void)myObjectAsArgument:(id<myObjectDelegate>)object;
with myObjectDelegate being the name of your protocol ;)

Accessing delegate of super class from subclass

I have four classes MainVC, ParentClient and ChildClient1, ChildClient2(which are subclasses of ParentClient). ParentClient has a delegate to MainVC such that in MainVC
- (void)viewDidLoad
{
[super viewDidLoad];
[ParentClient instance].mainViewDelegate = self;
}
And then the ParentClient looks like this
#interface BaseClient : NSObject
#property (assign) id<MainVCInteraction> mainViewDelegate;
+(instancetype) instance;
#end
Now I want to access mainViewDelegate from ChildClient1, ChildClient2 and it returns me nil while [ParentClient instance].mainViewDelegate returns the correct value
Here is what I did I removed the BaseClient Class so that ChildClient1, ChildClient2 were no longer subclasses of BaseClient. I defined a objective-c protocol file MainVCInteaction.h and made Client1, Client2 look like this:
#import "MainVCInteraction.h"
#interface ChildClient1 : NSObject
#property (assign) id<MainVCInteraction> mainViewDelegate;
+(instancetype) instance;
#end
#import "MainVCInteraction.h"
#interface ChildClient2 : NSObject
#property (assign) id<MainVCInteraction> mainViewDelegate;
+(instancetype) instance;
#end
And then MainVC implements this protocol, I assigned the delegate like this
- (void)viewDidLoad
{
[super viewDidLoad];
[ChildClient1 instance].mainViewDelegate = self;
[ChildClient2 instance].mainViewDelegate = self;
}

Cant call delegate method from my protocol class

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?
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!
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);
}

Resources