IOS app - protocol definition in a separate file

I defined the 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 made 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 that uses the protocol:

secondViewController.h

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

secondViewController.m

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

but my secondViewController does not call the metad protocol. Why is this so? Any suggestion would be appreciated.

+6
source share
1 answer

First, as @Abizern , try reformatting your code a bit. Use capital letters for classes. Said this is the solution here for your answer.

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

 #import <Foundation/Foundation.h> @protocol MyProtocol <NSObject> - (void)doSomething; @end 

This is a 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 completeness, be sure to check out the capabilities of the delegate template. Apple doc is your friend. You can take a look at the-basics-of-protocols-and-delegates to have a basic introduction to the argument. In addition, a SO search allows you to find many answers to this topic.

Hope this helps.

+11
source

Source: https://habr.com/ru/post/920480/


All Articles