9

I have defined a protocol in a separate file (myProtocol.h). Here is the code for it:

#import <Foundation/Foundation.h>

@protocol myProtocol <NSObject>
    -(void) loadDataComplete;
@end

Now I want to call this method so I have done the following code:

firstViewController.h:

#import "myProtocol.h"

@interface firstViewController : UIViewController{
    id <myProtocol> delegate;
}
@property (retain) id delegate;
-(void) mymethod;

firstViewController.m

@implementation firstViewController
@synthesize delegate;

- (void)viewDidLoad {
    [self mymethod];
}

-(void) mymethod {
    //some code here...
    [delegate loadDataComplete];
}

I have another file where the protocol is also utilized:

secondViewController.h:

#import "myProtocol.h"
@interface secondViewController : UIViewController<myProtocol>{
}

secondViewController.m:

-(void) loadDataComplete{
    NSLog(@"loadDataComplete called");
}

but my secondViewController is not calling the protocol methad. Why is it so? Any suggestion will be appreciated.

2
  • 2
    Are you actually creating an object and assigning it as a delegate? Also - having a strong reference to a delegate is not usual. Finally, class names should start with a capital letter. Commented Jul 14, 2012 at 10:33
  • do you set your delegate , what's the value of self.delegate into mymethod message ? i have the habit to use "self." for property access Commented Jul 14, 2012 at 10:45

1 Answer 1

14

First, as @Abizern suggested, try to reformat your code a little bit. Use capital letter for classes. Said this here the solution for your answer.

This is the protocol. I would name it like FirstViewControllerDelegate since the class that implements the object is a delegate for FirstViewController.

#import <Foundation/Foundation.h>

@protocol MyProtocol <NSObject>

- (void)doSomething;

@end

This is SecondViewController.

#import <UIKit/UIKit.h>
#import "MyProtocol.h"

@interface SecondViewController : UIViewController <MyProtocol>

@end

@implementation SecondViewController

// other code here...

- (void)doSomething
{
    NSLog(@"Hello FirstViewController");
}

@end

This is FirstViewController.

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

// it coud be better to declare these properties within a class extension but for the sake of simplicity you could leave here
// the important thing is to not declare the delegate prop with a strong/retain property but with a weak/assign one, otherwise you can create cycle
@property (nonatomic, strong) SecondViewController* childController;
@property (nonatomic, weak) id<MyProtocol> delegate;

@end

@implementation FirstViewController

// other code here...

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.childController = [[SecondViewController alloc] init];
    self.delegate = self.childController; // here the central point

    // be sure your delegate (SecondViewController) responds to doSomething method
    if(![self.delegate respondsToSelector:@selector(doSomething)]) {

        NSLog(@"delegate cannot respond");
    } else {

        NSLog(@"delegate can respond");
        [self.delegate doSomething];
    }    
}

@end

For the sake of completeness, be sure to understand the delegate pattern means. Apple doc is your friend. You could take a look at the-basics-of-protocols-and-delegates to have a basic intro on the argument. Furthermore, SO search allows you to find a lot of answers on the topic.

Hope that helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Obviously, the if-else pattern can be moved in myMethod if you want.
Thank you Flex_Addicted. Amazing answer. little bit correction in code self.delegate = self.childController; instead of self.delegate = child; thanks again.
@ViralNarshana You're welcome! Sorry for the mistake. I fixed it :)
I am confused. Shouldn't the delegate be on the SecondViewController instead of the FirstViewController?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.