Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:23:27+00:00 2026-06-07T17:23:27+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:23:29+00:00Added an answer on June 7, 2026 at 5:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a protocol like this: @protocol RSSItemParserDelegate <NSObject> - (void)RSSItemParser:(RSSItemParser *)parser didEndParsingSuccesfully:(BOOL)success;
I have defined a delegate protocol in my custom class this way: #import <Foundation/Foundation.h>
I have defined many constants in GlobalVar.h and other .h files. I import these
I have a little problem; I have this protocol defined as so below: @protocol
I have defined file path like- $docRoot = $_SERVER['DOCUMENT_ROOT']; $webRoot = str_replace(array($docRoot, 'library/config.php'), '',
I have the following code but the FB.login gives a FB is not defined
I have defined a protocol that all my plug-ins must implement. I would also
I have below a code that will plot a sphere, it's proportions are defined
I now have this code based on some of the answers below. Is this
I have a custom URL protocol for an application I'm working on (as defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.