implemented delegate method but told it's not implemented - objective-c

I'm trying to implement delegation. In the .h file of a custom class , I do this
#import <UIKit/UIKit.h>
#class Timer;
#protocol TimerDelegate
-(void)myClassDelegateMethod:(Timer *)timer;
#end
typedef void(^MyCustomBlock)(void);
#interface Timer : UILabel
#property (nonatomic, weak) id <TimerDelegate> delegate;
In the .m file I synthesize the delegate and also called the delegate method, checking to see first if the delegate implements the method
#synthesize delegate;
-(void)countdownTime:(NSTimer *)timer
{
NSLog(#"countdownTime called");
....
[self.delegate myClassDelegateMethod:self];
if (self.delegate != nil && [self.delegate respondsToSelector:#selector(myClassDelegateMethod:)]) {
[self.delegate performSelector:#selector(myClassDelegateMethod:)];
} else {
NSLog(#"Delegate doesn't implement myClassDelegateMethod");
}
when I run my code, I'm told the delegate doesn't implement the method. Here's how I implement it
In the viewController, I declare that it conforms to the protocol
#interface scViewController : UIViewController <TimerDelegate>
And then in the .m file of the viewController, I implement the delegate's method
- (void) myClassDelegateMethod:(Timer *) sender {
NSLog(#"Delegates are great!");
}
Can you explain how I've failed to implement the delegate method properly?
Update, in the viewController, I have a method that creates timer instances
-(Timer *)timer
{
_timer = [[Timer alloc] init];
return _timer;
}
In viewDidLoad, I do this
self.timer.delegate = self;

Your timer method is a problem. It should be:
-(Timer *)timer
{
if (!_timer) {
_timer = [[Timer alloc] init];
}
return _timer;
}
As you have it, every time you do self.timer you were creating a new timer so the delegate was only applied to one of the many instances.

Related

delegate method not getting called

I have looked at all the other questions with the same problem but I cannot seem to get my heard around it. I am pretty sure I have done everything correctly as this is not my first time using delegates.
//PDFView.h
#class PDFView;
#protocol PDFViewDelegate <NSObject>
-(void)trialwithPOints:(PDFView*)pdfview;
#end
#interface PDFView : UIView
#property (nonatomic, weak) id <PDFViewDelegate> delegate;
In the implementation file i am trying to call the delegate method from touchesMoved delegate of view
//PDFView.m
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.delegate trialwithPOints:self];
}
The class where the delegate method is implemented
//points.h
#import "PDFView.h"
#interface points : NSObject <PDFViewDelegate>
//points.m
//this is where the delegate is set
- (id)init
{
if ((self = [super init]))
{
pdfView = [[PDFView alloc]init];
pdfView.delegate = self;
}
return self;
}
-(void)trialwithPOints:(PDFView *)pdf
{
NSLog(#"THE DELEGATE METHOD CALLED TO PASS THE POINTS TO THE CLIENT");
}
So this is how i have written my delegate and somehow the delegate is nil and the delegate method is never called.
At the moment I am not doing anything with the delegate, I just want to see it working.
Any advices on this would be highly appreciated.
I think it is because you did not hold the reference to the instance of the delegate, and it got released because it is declared weak. You might be doing this:
pdfView.delegate = [[points alloc] init];
which you should fix to something like:
_points = [[points alloc] init];
pdfView.delegate = _points;
where _points is instance variable.

NSPanel not showing.

My approach to this may be all wrong so I appreciate your patience.
I have a button in my main XIB file linked to this method in my document.m file:
- (IBAction)showTagModal:(id)sender {
if (!_FileTagWindowController){
_FileTagWindowController = [[FileTagWindowController alloc]init];
}
[_FileTagWindowController showWindow:self];
}
_FileTagWindowController is declared as a property in document.h and using breakpoints when the method is called, as far as I can tell is initializing properly, however _windowNibName and _window remains nil.
FileTagWindowController.h looks like this.
#import <Cocoa/Cocoa.h>
#interface FileTagWindowController : NSWindowController{
}
#property (strong) IBOutlet NSArrayController *tagsArray;
- (IBAction)saveContext:(id)sender;
#end
FileTagWindowController.m looks like this:
#import "FileTagWindowController.h"
#interface FileTagWindowController ()
#end
#implementation FileTagWindowController
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
}
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
NSLog(#"Window Did Load!");
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
- (IBAction)saveContext:(id)sender {
}
#end
in my FileTagWindowController.xib I have File Owner set to FileTagWindowController as the custom class. I have the File Owner's "window" outlet linked to the window (NSPanel). That's all that should be required correct? The NSLOG statement in WindowDidLoad never gets called. I tried using [super initWithWindowNibName] in FileTagWindowController.m but that crashes not only the app, but Xcode as well with an endless initialization loop. Am I missing something obvious here?
Thanks all so much.
Try something like the following.
// document.h
#import "FileTagWindowController.h"
#property (strong) filetagWindowController *FileTagWindowController;
// document.m
#synthesize filetagWindowController;
- (IBAction)showTagModal:(id)sender {
if (self.filetagWindowController == nil) {
self.filetagWindowController = [[FileTagWindowController alloc] initWithWindowNibName:#"FileTagWindowController"];
}
[filetagWindowController showWindow:self];
[[filetagWindowController window] setReleasedWhenClosed:NO];
[NSApp runModalForWindow:filetagWindowController.window];
filetagWindowController = nil;
}
You may also want to call NSWindowWillCloseNotification to observe its state and see if filetagWindowController is closed.

delegate method does not get called

I'm completely stuck with calling a method from a UIView subclass, the method just doesn't get fired, I have a feeling that I'm doing something wrong but after searching the web I did not find any clue. Thank you in advance
Here's the iPadMainViewController.h file
#import <UIKit/UIKit.h>
#import "TouchView.h"
#interface iPadMainViewController : UIViewController <TouchViewDelegate>
#property (retain) UIWebView *detailsView;
#end
and the iPadMainViewController.h file that holds the method
- (void)MethodNameToCallBack:(NSString *)s
{
NSLog(#"%#",s);
}
Here's the TouchView.h file, which is supposed t
#protocol TouchViewDelegate
- (void)MethodNameToCallBack:(NSString *)s;
#end
#interface TouchView : UIView {
id<TouchViewDelegate> delegate;
}
#property (nonatomic, assign) id delegate;
#end
Here's the TouchView.m file which is supposed to call a method of it's delegate
#implementation TouchView
#synthesize delegate;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"HELLO FROM INSIDE");
[[self delegate] MethodNameToCallBack:(NSString *)#"HELLO FROM OUTSIDE"];
}
#end
Synthesizing the delegate is not enough, because it just creates the getter and the setter methods. It does not create an instance of iPadMainViewController.
So after you create an instance of TouchView, you should assign an instance of iPadMainViewController as the delegate.
iPadMainViewController *controller = [[iPadMainViewController alloc] init...
// ...
TouchView *touchView = [[TouchView alloc] init...
// ...
touchView.delegate = controller;
Or in the iPadMainViewController's viewDidLoad method:
- (void)viewDidLoad
{
// ...
self.touchView.delegate = self;
}
check after you instantiated a TouchView instance, did you assign its delegate?
Enhance your touchesBegan implementation a little for further debugging:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"HELLO FROM INSIDE");
NSLog(#"our delegate is set towards: %#", delegate);
...
}
Does it log something useful in that second logging statement?
I presume it prints nil and that would be the root cause of your issue; you forgot to assign the delegate.

Objective-C – Retained property after being set is nil?

I have two classes:
A UIViewController and a class that's subclassing NSObject that acts as a downloading helper class called OfficesParser. OfficesParser is using ASIHTTPRequest and I set the delegate for the download requests to be my UIViewController.
EDIT: Interface for the UIViewController:
#interface OfficesViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, ASIHTTPRequestDelegate> {
OfficesParser *officesParser;
}
#property (nonatomic, retain) OfficesParser *officesParser;
#end
In the UIViewController implementation I set up the OfficesParser like so:
- (void)viewDidLoad {
[super viewDidLoad];
self.officesParser = [[[OfficesParser alloc] init] autorelease]; // self.officesParser is retained
}
Then before the view appears I call my my OfficesParser object to download some data for me:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.officesParser doNetworkOperations];
}
Also in my UIViewController I have setup the appropriate delegate methods to deal with the data after it has been downloaded. In particular I'm interested in this delegate method that will run after all the data has been processed in my download queue. I can see that the delegate method is running from the log. But for some reason self.officesParser in here is nil.
- (void)queueFinished:(ASINetworkQueue *)queue {
DLog(#"queueFinished running");
[self.officesParser test]; // test will not get called because self.officesParser is nil
}

Method implemented from delegate not triggered

I have a UIViewController which is embedded in a navigation controller and presented modally:
//UIViewController
AuthenticationController *auth = [[AuthenticationController alloc] init];
//UINavigationController
AuthRootController *navController = [[AuthRootController alloc]
initWithRootViewController:auth];
navController.navigationBar.topItem.title = #"Anmelden";
navController.delegate = self;
[self presentModalViewController:navController animated:YES];
RELEASE_SAFELY(navController);
However there is something wrong with the delegate I created within the AuthRootController class:
#protocol AuthRootControllerDelegate
#required
-(void)authRootControllerDidEnd:(UINavigationController *)sender;
#end
#interface AuthRootController : UINavigationController {
id<AuthRootControllerDelegate> delegate;
}
#property (nonatomic, assign) IBOutlet id delegate;
#end
And the implementation:
#implementation AuthRootController
#synthesize delegate;
-(void)userDidCancelController:(UINavigationController *)sender{
if (self.delegate && [self.delegate conformsToProtocol:#protocol(AuthRootControllerDelegate)]) {
[self.delegate authRootControllerDidEnd:sender];
}
}
#end
When I use the method
-(void)authRootControllerDidEnd:(UINavigationController *)sender
it is not triggered. Any ideas?
Have you declared that your delegate conforms to AuthRootControllerDelegate? The conformsToProtocol test looks at whether the delegate declares conformance, it doesn't do any sort of method-by-method check. So even if you've implemented authRootControllerDidEnd: on your delegate, conformsToProtocol can still return NO.
In your interface you aren't declaring it as implementing the delegate protocol, you need to modify your interface declaration like this:
#interface AuthRootController : UINavigationController<AuthRootControllerDelegate> {