Alexidze
Alexidze

Reputation: 183

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

Upvotes: 1

Views: 1578

Answers (3)

sch
sch

Reputation: 27536

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;
}

Upvotes: 3

Till
Till

Reputation: 27597

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.

Upvotes: 0

Chris Chen
Chris Chen

Reputation: 5407

check after you instantiated a TouchView instance, did you assign its delegate?

Upvotes: 0

Related Questions