4

i've worked with delegation before. i know how to create delegation from a superview to a subview class. however, i'm trying to do it the opposite way using the same approach but it's not working! is delegation meant only to work one way or is there a way/trick to use it as a two way communication between the classes? I'm receiving an error at the parent/superview .h class which is:

Cannot find protocol definition for 'SubViewControllerDelegate'

my code goes like this: subview.h

#import <UIKit/UIKit.h>

#import "SuperViewController.h"

@protocol SubViewControllerDelegate <NSObject>

    - (void)someMethod:(NSData *)data;

@end

@interface SubViewController : UIViewController 

@property (weak, nonatomic) id <SubViewControllerDelegate> delegate;

@end

subview.m:

[self.delegate someMethod:data]; 

SuperView.h

#import <UIKit/UIKit.h>

#import "SubViewController.h"

@interface SuperViewController : UIViewController <SubViewControllerDelegate>

@end

SuperView.m:

#pragma mark - SubView Controller Delegate Methods
- (void)someMethod:(NSData *)data{
    NSLog(@"%@", data);
}

am i doing anything wrong or missing out anything?

user1938695
  • 623
  • 1
  • 5
  • 14

1 Answers1

5

You have an "import-cycle", because "SuperViewController.h" imports "SubViewController.h" and vice versa.

Removing the #import "SuperViewController.h" in "SubViewController.h" should solve the problem.

If you really need that class to be declared in "SubViewController.h", use @class SuperViewController; to avoid the import-cycle.

Remark: The <SubViewControllerDelegate> protocol declaration is probably not needed in the public interface "SuperViewController.h" at all.

In "SuperViewController.h", declare the class as

@interface SuperViewController : UIViewController

In "SuperViewController.m", define a class extension with the protocol:

@interface SuperViewController () <SubViewControllerDelegate>
@end
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • thanks it worked! i need to remember that for later :) oh, btw the delegate method isn't being called is there anything missing in the SubView.m file or SuperView.m ? i have a feeling i need to do something to the self.delegate in the Subview.m – user1938695 Jul 12 '13 at 08:06
  • 1
    @user1938695: Do you *assign* a value to self.delegate somewhere? – Martin R Jul 12 '13 at 08:08
  • yes in the subview.m file i added a line [self.delegate someMethod:data]; – user1938695 Jul 12 '13 at 08:12
  • 1
    @user1938695: That is *calling* the delegate function. You have to *assign* a value, otherwise `self.delegate` is `nil`. Perhaps in "SuperViewController.m": `subview.delegate = self` or similar. – Martin R Jul 12 '13 at 08:17
  • i've tried to assign the value but it's not working! i'm sure i'm typing something wrong though. any hints? – user1938695 Jul 12 '13 at 08:36
  • okay just before posting the code, what did you mean by "...or similar" ? i actually didn't know where to assign a value i should create an object of the class subview to access the delegate. in my app i'm using modal segue to navigate to the view that's using the subview class. so the code that i wanted to post was like and i wrote it in the ViewDidLoad: Subview *sv = [[Subview alloc] init]; [sv.delegate self]; i feel that it's totally incorrect. – user1938695 Jul 12 '13 at 08:43
  • `Subview *sv = [[Subview alloc] init]; sv.delegate = self;` should do the trick. – Martin R Jul 12 '13 at 08:50
  • i've tried it first but didn't use it since it was showing me this error: Assigning to 'id' from incompatible type 'SubView *const __strong. is it right to do that in the viewDidLoad? i guess i shouldn't be doing that there because there is no connection with the delegation method nor the subview i'm accessing after the parent. – user1938695 Jul 12 '13 at 08:54
  • @user1938695: I thought that code would be in SuperViewController.m. – Martin R Jul 12 '13 at 08:56
  • okay my bad. it worked without errors but can't access the delegation method in the superview.m yet... :( – user1938695 Jul 12 '13 at 08:57