Delegate method not called objective c - ios

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!

Related

Delegate function not called in AppDelegate

I have written protocol in a view controller, and implement it in AppDelegate, and when I call delegate function from view controller, the delegate function is not called. Below is my code -
In class AuthenticationViewController -
#class AuthenticationViewController;
#protocol ApplicationTiomeoutDelegate <NSObject>
-(void) addObserverForTimeout;
#end
And call this function using delegate -
[self.appTimeoutDelegate addObserverForApplicationTimeout];
And in AppDelegate, I have implemented this protocol like this -
#interface AppDelegate () <ApplicationTiomeoutDelegate>
#end
And then set delegate to self -
AuthenticationViewController *timeoutDelegate = [[AuthenticationViewController alloc] init];
[timeoutDelegate setAppTimeoutDelegate:self];
And implemented delegate function as well in AppDelegate, which is never called somehow -
-(void) addObserverForApplicationTimeout{
// this function is never called
}
I am not sure what is not correct here.
AppDelegate being a singleton need not have the ApplicationTiomeoutDelegate protocol invocation. You can directly invoke addObserverForTimeout
You create method in your appdelegate, you can directly call it wherever you want with instance of appdelegate.
For your question, check this below
Where are creating instance for Your delegate and where are calling your delegate form your viewcontroller, it is used to communicate between two class, it should have reference of protocol.
try this
Your viewcontroller.h
#protocol customDelegate;
#import <UIKit/UIKit.h>
#import "CustomTableViewCell.h"
#interface ViewController : UIViewController<UITableViewDelegate>
{
IBOutlet UITableView *mainTableView;
}
#property (nonatomic,strong) id <customDelegate> delegate;
#end
#protocol customDelegate <NSObject>
-(void)method;
#end
ViewController.m
- (void)viewDidLoad {
[self.delegate method];
}
Your app delegate
#interface AppDelegate () <customDelegate>
#end
Your didfinishlaunghing
viewController = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
viewController.delegate = self;
and implement method:
-(void)method{
NSLog(#"calling");
}

ios delegate method not being called

I have two Classes - say Class1 and Class2
Class1 has button which when pressed does a push segue to Class2.
Also, when the button is pressed a network call is being done on another class.
Class1 code :
- (IBAction)sellPressed:(id)sender
{
SPNNetworkEngine *networkEngine = [[SPNNetworkEngine alloc] init];
networkEngine.delegate = self;
[networkEngine getFarePrice];
}
In Network class, I have :
In .h file
#protocol SPNNetworkEngineDelegate <NSObject>
- (void) farePriceReceivedWithDetails:(id) farePrice;
#end
#property (nonatomic, weak) id delegate;
- (void) getFarePrice;
In .m file
- (void) getFarePrice
{
...........
.......
//After all the operations, I delegate
if ([delegate respondsToSelector:#selector(farePriceReceivedWithDetails:)])
[delegate farePriceReceivedWithDetails:cost];
}
}
And in Class2 I have,
- (void)viewDidLoad
{
[super viewDidLoad];
SPNNetworkEngine *networkEngine = [[SPNNetworkEngine alloc] init];
networkEngine.delegate = self;
}
// Delegate method
- (void) farePriceReceivedWithDetails:(id)farePrice
{
NSLog(#"FarePrice %#", farePrice);
}
The delegate method in Class2 is never called. What have I done wrong here?
But when I write the delegate method -(void)farePriceReceivedWithDetails:(id)farePrice in Class1, its working fine.
Your Coding approch is wrong You alloc SPNNetworkEngine class in Your Class1 and set delegate of SPNNetworkEngine is self. so The delegate is fire in class1. But you want this method in class2 so you have to assign this delegate with class2 object.
Second point is you have to Change this line with this
#protocol SPNNetworkEngineDelegate <NSObject>
- (void) farePriceReceivedWithDetails:(id) farePrice;
#end
#property (nonatomic, strong) id <SPNNetworkEngineDelegate> delegate;
- (void) getFarePrice;
You should retain this object in Class1.h as instance variable. So that it will be alive till that class object exist. What you are doing currently is simply allocating it in a method. once the method executed & finished, it will be deallocated. So your delegate call back will not work.
So avoid this line
SPNNetworkEngine *networkEngine = [[SPNNetworkEngine alloc] init];
change the above code to like below
Declare in Class1
#property (nonatomic, strong) SPNNetworkEngine *networkEngine;
Then do
self.networkEngine = [[SPNNetworkEngine alloc] init];
In the Class1 you are calling to [networkEngine getTestPrice] and I think that mean the [networkEngine getFarePrice] function (because is where you call the delegate)
In Class2 you must call the same function: [networkEngine getTestPrice]
Have you added the SPNNetworkEngineDelegate to the list of delegates in the Class2.h?
i.e.
#interface Class2 : UIViewController <SPNNetworkEngineDelegate>

iOS delegation doesn't work

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.

Why isn't my delegate object responding to method calls?

I ultimately want to write an iOS app incorporating ALAssetsLibrary, but as a first step toward understanding delegation, I'm trying to pass a simple message between two view controllers. For some reason, I can't seem to get the message to pass. In particular, the delegate object (derpy) doesn't appear to exist (if(self.derpy) returns NO)).
I asked the same question on the Apple forums and was told that I should be using segues and setting properties / calling methods using self.child instead, but that seems strange. If I were to pass messages using the parent / child properties, would I still be able to create my views in Interface Builder? Once I have my two views set up, say inside a UINavigationController, I'm not sure how to actually "wire them up" so I can pass messages between them. Sorry if the question is overly broad.
Here's the controller I'm declaring the protocol in (called PickerViewController):
Interface:
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
#protocol DerpDelegate <NSObject>
#required
- (void) test;
#end
#interface PickerViewController : UIViewController
#property (nonatomic, assign) id<DerpDelegate> derpy;
#end
Implementation:
#import "PickerViewController.h"
#interface PickerViewController ()
#end
#implementation PickerViewController
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.derpy) { // If the delegate object exists
[self.derpy test]; // send it this message
} else {
NSLog(#"Still not working."); // This always returns (i.e., self.derpy doesn't exist)
}
}
Delegate controller (MainViewController) interface:
#import <UIKit/UIKit.h>
#import "PickerViewController.h"
#interface MainViewController : UIViewController <DerpDelegate> // public promise to implement delegate methods
#property (strong, nonatomic) PickerViewController *picker;
- (void) test;
#end
And lastly, the delegate controller (MainViewController) implementation:
#import "MainViewController.h"
#import "PickerViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
// Here's that method I promised I'd implement
- (void) test{
NSLog(#"Test worked."); // This never gets called
}
- (void)viewDidLoad {
[super viewDidLoad];
self.picker.derpy = self;
//lazy instantiation
- (PickerViewController *) picker{
if(!_picker) _picker = [[PickerViewController alloc]init];
return _picker;
}
EDIT: Many thanks to rydgaze for pointing me in the right direction with self.picker.derpy = self, but for some reason, things still aren't working properly. Importantly, once that property has been set, if(self.picker.derpy) returns YES from MainViewController. But if(self.derpy) is still returning NO when called from inside the PickerViewController's viewDidLoad. How can the property exist and not exist at the same time?
You need to be sure that you're setting the delegate on the instance of the view controller that you put on screen. If you're using a navigation controller and segues to go between MainViewController and PickerViewController, then you should set the delegate in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
self.picker = (PickerViewController *)segue.destinationViewController;
self.picker.derpy = self;
}
You need to populate the delegate first.
Basically, your MainViewController shoudl at somepoint do a
picker.derpy = self;
Then when the delegate fires in PickerViewController, the callback will happen.
Edit:
A good practice is to do something like in PickerViewController
#property (nonatomic, assign) id<DerpDelegate > derpy;
and in your MainViewController indicate that you will implement the delegate
#interface MainViewController : UIViewController<DerpDelegate>
Eventually in your implementation of MainViewController
You will have something like
picker = [[PickerViewController alloc]init];
picker.derpy = self;
[picker doYourThing];
Once picker is all done, it may want to return results using the delegate.

iOS - How to implement self created delegate in ios6

i have created a delegate for my project the code of my main view is
VedantViewController.h
#protocol VedantDelegate;
#interface VedantViewController : UIViewController
{
id <VedantDelegate> delegate;
}
//some other outlets
#property(nonatomic, assign) id <VedantDelegate> delegate;
#protocol VedantDelegate <NSObject>
- (void)display:(NSString *)JSONResponse;
#end
VedantViewController.m
#synthesize delegate;
[delegate display:jsonResponse];
SecondViewController.h
#interface SecondViewController : UIViewController<VedantDelegate>
- (void)display:(NSString *)JSONResponse;
SecondViewController.m
- (void)display:(NSString *)string
{
}
but this code is not working properly
when i debug the code using breakpoints the code reaches the
[delegate display:abc];
but it does not calls display function in SecondViewController.m file
i think my code is right but some mistake that i can't recognize
let me explain you the flow of my project this could be the problem
by default the VedantViewController view is launched
after that when the show button is click it calls the SecondViewController view in the view these is list button that calls the function in VedantViewController this function then calls the delegate method that is [delegate display:jsonResponse];
Thanks in Advance,
Arun.
The view controller which is confirming with the protocol, should have this line in the viewDidLoad or anywhere you are making the object of that viewController
Add this line in SecondViewController.m
VedantViewControllerObject.delegate = self;
#protocol VedantDelegate;
#interface VedantViewController : UIViewController{
id<VedantDelegate> delegate;
}
//some other outlets
#property(nonatomic,assign) id<VedantDelegate> delegate;
#protocol VedantDelegate <NSObject>
-(void)displayAccounts:(NSString *)JSONResponse;
-(void)display:(NSString *)JSONResponse;
#end
and also set delegate to object of VedantViewControllerObject class as self in SecondViewController class and the object of VedantViewControllerObject class should be initialized and allocated.
vedantViewControllerObject.delegate = self;
In your VedantViewController.h file you declared method as below
-(void)displayAccounts:(NSString *)JSONResponse;
But you are calling it [delegate display:jsonResponse];
You just try to call
[delegate displayAccounts:jsonResponse];
And in SecondViewController.m
(void)displayAccounts:(NSString *)string{
}
There are some issues in your code:
set the delegate in second view controller
vedViewObject.delegate = self;
You added displayAccounts method in delegate and calling display method, that can cause issues. If that methods are not implemented in the delegate class.
Add if condition like: if(delegate)[delegate displayAccounts:jsonResponse];

Resources