// 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.
Related
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 saw a lot of this kind of questions and answers here, but couldn't find solution to my problem. I'm trying to send data from one view controller to another and use delegate. But don't know why my postDelegate doesn't responds to selector. Is something wrong with this code or what is the problem?
PostViewController.h file
#protocol GetDataDelegate <NSObject>
-(void)getPassedInfo:(NSString*)info;
#end
#interface PostViewController : UIViewController
#property (nonatomic, weak) id <GetDataDelegate> postDelegate;
#end;
PostViewController.m file
#import "PostViewController.h"
- (IBAction)postData:(id)sender {
if ([_postDelegate respondsToSelector:#selector(getPassedInfo:)]) {
[self.postDelegate getPassedInfo:#"data"];
NSLog(#"responds");
}
[self dismissViewControllerAnimated:YES completion:nil];
}
in second view controllers .h file
#import "PostViewController.h"
#interface MainViewController : UITableViewController <GetDataDelegate>
and in .m file
#implementation MainWindowTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
PostViewController * postController = [[PostViewController alloc]init];
postController.postDelegate = self;
}
and here is delegate method:
-(void)getPassedInfo:(NSString *)info{
NSLog(#"info is %#", info);
}
You need to make postController a property or an ivar. Currently it is a local variable in the viewDidLoad method which will be deallocated after viewDidLoad completes as #CodaFi said above.
#import "PostViewController.h"
#interface MainViewController : UITableViewController <GetDataDelegate>
#property (nonatomic, strong) PostViewController *postController;
#end
Then:
- (void)viewDidLoad
{
[super viewDidLoad];
self.postController = [[PostViewController alloc]init];
self.postController.postDelegate = self;
}
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);
}
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.