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]);
}
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 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);
}
Hi in this code the protocol RefreshLibraryDropBoxDelegate doesn't work, doesn't call the method refreshLibrary in the WVdALibraryDocumentViewController. Why?
WVdADropboxViewController.h:
#import <UIKit/UIKit.h>
#import <DropboxSDK/DropboxSDK.h>
#protocol RefreshLibraryDropBoxDelegate <NSObject>
#optional
-(void)refreshLibrary;
#end
#interface WVdADropboxViewController : UIViewController <DBRestClientDelegate, UITableViewDataSource, UITableViewDelegate>
{
id <RefreshLibraryDropBoxDelegate> delegate;
}
//delegate
#property (assign) id <RefreshLibraryDropBoxDelegate> delegate;
WVdADropboxViewController.m:
- (void)restClient:(DBRestClient *)client loadedFile:(NSString *)destPath
{
NSLog(#"upload complete");
[self.delegate refreshLibrary];
[[self navigationController] popViewControllerAnimated:YES];
}
WVdALibraryDocumentViewController.h:
#import <UIKit/UIKit.h>
#import "WVdACustomCell.h"
#import "WVdAViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "WVdADropboxViewController.h"
#interface WVdALibraryDocumentViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, RefreshLibraryDropBoxDelegate>
-(void)refreshLibrary;
WVdALibraryDocumentViewController.m:
// REFRESH LIBRARY //
-(void)refreshLibrary
{
NSLog(#"refresh");
[self getDataArrayDocumentFiles];
}
You do not need this line
{
id <RefreshLibraryDropBoxDelegate> delegate;
}
Then, in WVdALibraryDocumentViewController
you need to set in viewDidLoad, or somewhere else
WVdADropboxViewController *myCoolController = [WVdADropboxViewController new];
myCoolController.delegate = self;
It should work!
I guess you for get to set the delegate inside your WVdALibraryDocumentViewController.m.
In your viewDidLoad in WVdALibraryDocumentViewController:
wVdADropboxViewController.delegate = self;
I've a main class where I want to define two protocols (1 used by a class A, the other by class B) (ios 6.1, xcode 4.6.3 , ARK mode, storyboard project).
According to official syntax, all my code seems to be correct.
But when I try to use the second delegate , nothing work correctly, my 2nd delegate does not respond
**HEADER myProtocols.h**
#import ...
#class myProtocols;
#protocol myProtocol1 <NSObject>
// list of methods and properties
doStuff:(float) myValue;
#end
#protocol myProtocol2 <NSObject>
// list of methods and properties
doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
#end
#interface myProtocols:NSObject
{
__unsafe_unretained id <myProtocol1> _myDelegate1;
__unsafe_unretained id <myProtocol2> _myDelegate2;
}
#property (nonatomic, assign) id <myProtocol1> myDelegate1;
#property (nonatomic, assign) id <myProtocol2> myDelegate2;
#end
**MESSAGES myProtocols.m**
#import myProtocols.h
#implementation myProtocols
#synthesize myDelegate1 = _myDelegate1
#synthesize myDelegate2 = _myDelegate2
...
if ([_myDelegate1 respondsToSelector:#selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:#selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:#"YES MAN" andType:#"YES BRO"];
**// THIS DELEGATE DONT WORK, IT'S LIKE IT DOESNT INIT**
...
#end
**HEADER classA.h**
#import "myProtocols.h"
#interface classA: UIViewController <myProtocol1>
#property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doStuff:(float) myValue; according to comments, nothing to do :(
#end
**MESSAGES classA.m**
#import "classA.h"
#interface classA ()
#end
#implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
}
-(void) doStuff:(float) myValue
{
NSLog(#" YES VALUE IS %f",myValue);
}
**HEADER classB.h**
#import "myProtocols.h"
#interface classB: UIViewController <myProtocol2>
#property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType; according to comments, nothing to do :(
#end
**MESSAGES classB.m**
#import "classB.h"
#interface classB ()
#end
#implementation classB
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate2 = self;
}
-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
{
NSLog(#" YES VALUE IS %f and text %# and type %#",myValue2,myText,myType);
}
So, my mistake is to call [_myDelegate2 doOtherStuff..] directly inside a function in myProtocols called only by classA.
Then, if I want to call a function in myProtocols using both delegate I MUST init both these delegates in the class (A or B doesnt matter) I use to call this function:
**MESSAGES myProtocols.m**
#import myProtocols.h
#implementation myProtocols
#synthesize myDelegate1 = _myDelegate1
#synthesize myDelegate2 = _myDelegate2
-(void) pleaseDoIt
{
if ([_myDelegate1 respondsToSelector:#selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:#selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:#"YES MAN" andType:#"YES BRO"];
**// THIS DELEGATE NOW WORK VERY WELL**
}
#end
**HEADER classA.h**
#import "myProtocols.h"
#import "classB.h"
#interface classA: UIViewController <myProtocol1>
#property(strong, nonatomic) myProtocols *myProtoVC;
#property(strong, nonatomic) classB *classBVC;
//-(void) doStuff:(float) myValue;
#end
**MESSAGES classA.m**
#import "classA.h"
#interface classA ()
#end
#implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_classBVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
_myProtoVC.myDelegate2 = _classBVC // THIS IS THE POINT!!!
[_myProtoVC pleaseDoIt];
}
-(void) doStuff:(float) myValue
{
NSLog(#" YES VALUE IS %f",myValue);
}