Why is my delegate method only called in MainViewController and not in MapViewController?
LocationService.h
#protocol LocationServiceDelegate <NSObject>
#optional
- (void) currentLocation: (CLLocation*) location;
#end
#property (nonatomic, assign) id delegate;
LocationService.m
if ([delegate respondsToSelector:#selector(currentLocation:)])
{
[delegate currentLocation:newLocation];
}
Here the method get called (in MainViewController)
MainViewController.h
#import "LocationService.h"
#interface MainViewController : UIViewController <LocationServiceDelegate>
MainViewController.m
locationService = [[LocationService alloc] init];
locationService.delegate = self;
- (void) currentLocation: (CLLocation*) location
{
// some code, this method get called perfectly
}
And in my other class, the same procedure wont work
MapViewController.h
#import "LocationService.h"
#interface MapViewController : UIViewController <MKMapViewDelegate, LocationServiceDelegate>
MapViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
locationService =[[LocationService alloc] init];
locationService.delegate = self;
}
- (void)currentLocation:(CLLocation *)location
{
// this method does not get called
}
Cheers!
Are you setting delegate to nil somewhere as the code looks fine to me? Please use break pointer to see if it step into currentLocation method implementation in MapViewController.
Related
// userprofileform.h(viewcontroller)
#class UserProfileForm;
#protocol UserProfileFormDelegate <NSObject>
- (void) UserProfileFormDelegateReload: (UserProfileForm *) sender;
#end
#interface UserProfileForm : OTSFormViewController <UIActionSheetDelegate>
#property(nonatomic,assign) id <UserProfileFormDelegate> delegate;
#end
//userprofileform.m
#import "UserProfileForm.h"
#implementation UserProfileForm
- (void)endFlow:(id)sender {
[self.delegate UserProfileFormDelegateReload:self];
[self.navigationController popViewControllerAnimated:YES];
}
//shopprofilecontroller.h
#import "UserProfileForm.h"
#interface ShopProfileController : OTSViewController <UserProfileFormDelegate>
#end
//shoprofilecontroller.m
#implementation ShopProfileController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"Business";
UserProfileForm * userProfileForm = [[UserProfileForm alloc] init];
userProfileForm.delegate = self;
}
-(void)UserProfileFormDelegateReload:(UserProfileForm *)sender{
NSLog(#"delegates fired");
}
but the delegate method is not called upon popping the UserProfileForm.
I don't know why is it not firing. I have tried NSNotifications and it did not fire too.
I just tried your code and it worked fine.
The only thing I changed was at the end of viewDidLoad() of ShopProfileController I put the following line:
[userProfileForm endFlow:self];
So your delegate implementation should be correct.
Set a breakpoint within your endFlow() method, I bet its not getting called.
I'm having trouble figuring out why I'm getting a nil delegate for this call. It seems like I'm setting the delegate just fine.
LogoutViewController.h
#protocol SomeNewDelegate <NSObject>
#required
- (void)someMethod;
#end
#interface LogoutViewController : UIViewController
#property (nonatomic, weak) id<SomeNewDelegate> delegate;
LogoutViewController.m
- (IBAction)logoutButtonTapped:(id)sender {
NSLog(#"logout tapped");
[self.delegate someMethod];
[self dismissViewControllerAnimated:YES completion:nil];
}
MainViewController.m
#interface MainViewController () <UIScrollViewDelegate, SomeNewDelegate>
#property (nonatomic, strong) LogoutViewController *logoutVC;
#end
#implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.logoutVC.delegate = self;
}
- (void)someMethod {
NSLog(#"someMethod");
}
someMethod never gets called and I don't see why the delegate is nil. Any help?
Use below code in viewDidload
LogoutViewController *logoutVC = [[LogoutViewController alloc]init];
logoutVC.delegate = self;
Remove LogoutViewController from property.
I have used this and its working fine.
Hope its working for you.
Edited code in .h file
#protocol syncDataDelegate <NSObject>
#required
- (void) offlineSyncProcess;
#end
#interface LoginViewController : UIViewController
{
id <syncDataDelegate> syncDelegate;
}
#property (retain) id syncDelegate;
in .m file
#implementation LoginViewController
{
// your code
}
#synthesize syncDelegate;
- (IBAction)logoutButtonTapped:(id)sender {
NSLog(#"logout tapped");
[syncDelegate offlineSyncProcess];
}
You can call delegate method from here in LoginViewController.m file from any method.
in My Delegate implementation
in .m file
#interface MainViewController ()<syncDataDelegate>
#end
#implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
LoginViewController *LVC = [[LoginViewController alloc]init];
LVC.syncDelegate = self;
}
now use the delegate methode here
- (void) offlineSyncProcess
{
// your code here
}
this code is working in my project, check this in your project.
Make sure your logoutVC is not nil when you try to set the delegate property for it.
You can implement the setDelegate method in your LogoutViewController.m. Then use break point to make sure it is invoked.
Delegate is nil because your logoutVC is nil, you haven't allocated memory to the logoutvc property.
Just before self.logoutVC.delegate = self
Add following code
self.logoutVC = [[LogoutViewController alloc]init];
Everything will start working automatically.
I have this delegate that is not working out as planned, I have it setup like so. I want to call the function NSLog(show); I am not too sure why this isn't work, but works with my other viewcontroller. I must be missing some small detail.
AccountViewController.h
#protocol AccountViewControllerDelegate;
#interface AccountViewController : UIViewController{
}
#property (nonatomic, assign) id <AccountViewControllerDelegate> accountViewDelegate;
#end
#protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
#end
AccountViewController.m
-(IBAction)save:(id)sender {
[self showLabel];
}
- (void)showLabel {
if (self.accountViewDelegate) {
NSLog(#"showlabel");
[self.accountViewDelegate showLabel];
}
}
MapViewController.m
-(void)showLabel {
NSLog(#"SHOW");
}
you haven't shown where you assign the MapViewController to be the delegate of the AccountViewController. Perhaps thats what you are missing
//(from somewhere in the MapViewController)
AccountViewController *accountVC = //however you instantiate it (segue, storyboard etc
accountVC.accountViewDelegate = self;
Kindly note that delegate should not have strong reference.
So use
#property (unsafe_unretained) id <AccountViewControllerDelegate> accountViewDelegate;
Now in MapViewController.m or MapViewController.h conform to this protocol as
#interface MapViewController : UIViewController <AccountViewControllerDelegate>{
}
Then in MapViewController.m please do
AccountViewController *accountVC = [[AccountViewController alloc]init]; // initialize it with whatever be like storyboard or nib
accountVC.accountViewDelegate = self;
Your AccountViewController.h file should look like this
#protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
#end
#interface AccountViewController : UIViewController{
}
#property (unsafe_unretained) id <AccountViewControllerDelegate> accountViewDelegate;
#end
Here what you should have in your different files:
AccountViewController.h
Note that "assign" is mainly for primitives like NSInteger, CGFloat, BOOL (that is, for properties that are not objects). Use "weak" to keep a pointer toward your delegate without incrementing his retain count.
#protocol AccountViewControllerDelegate;
#interface AccountViewController : UIViewController
#property (nonatomic, weak) id <AccountViewControllerDelegate> accountViewDelegate;
#end
#protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
#end
AccountViewController.m
Here, it is usually good practice to check if the delegate isn't nil and if it has the method you want to call implemented. Use respondToSelector for that.
-(IBAction)save:(id)sender {
[self showLabel];
}
- (void)showLabel {
if (self.accountViewDelegate && [self.accountViewDelegate respondsToSelector:#selector(showLabel)]) {
NSLog(#"showlabel");
[self.accountViewDelegate showLabel];
}
}
MapViewController.h
Be sure to import "AccountViewController.h" here.
#import "AccountViewController.h"
#interface MapViewController : UIViewController <AccountViewControllerDelegate>
#end
MapViewController.m
Put the following where you were instantiating your AccountViewController object:
//Somewhere
AccountViewController *accountViewController = [[AccountViewController alloc] init];
accountViewController.accountViewDelegate = self;
and this is your delegate's method implementation:
- (void)showLabel {
NSLog(#"SHOW");
}
I'm having strange problem, trying to call my delegate - it never gets called. As i understand ,delegate never gets set. i'm using ARC.
CueCreateDelegate.h
#import <Foundation/Foundation.h>
#class CueCreateDelegate;
#protocol CueCreDelegate <NSObject>
- (void)CueCreatedCall;
#end
#interface CueCreateDelegate : NSObject
#property (nonatomic, assign) id <CueCreDelegate> delegate;
-(void)CueCreated;
- (void) setdelegate:(id<CueCreDelegate>) delegates; //for testing
#end
CueCreateDelegate.m
CueCreated is called from another class and it prints the bad news...
#import "CueCreateDelegate.h"
#implementation CueCreateDelegate
#synthesize delegate ;
- (void) setdelegate:(id<CueCreDelegate>) delegates{
delegate = delegates;
}
-(void)CueCreated{
if ([delegate respondsToSelector:#selector(CueCreatedCall)]) {
[delegate CueCreatedCall];
}
else{
NSLog(#"Delegate method not getting called...%#",delegate);
}
}
#end
MatrixViewController.h
#import <UIKit/UIKit.h>
#import "CueCreateDelegate.h"
#interface MatrixViewController : UIViewController<UIGestureRecognizerDelegate,CueCreDelegate>{
CueCreateDelegate *cueCre;
}
MatrixViewController.m
CueCreatedCall is never called..
-(void)CueCreatedCall{
NSLog(#"lsakdlakjdlks");
}
- (void)viewDidLoad
{
cueCre = [[CueCreateDelegate alloc] init];
// [cueCre setdelegate:self];
[cueCre setDelegate:self];
NSLog(#"%#",[cueCre delegate]);
}
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);
}