Search code examples
c++iosobjective-cdelegatesprotocols

Objective-C protocol delegate not working


I want to send back an array of objects to myViewController from myRenderer

In myRenderer.h:

@protocol myProtocol;

@interface myRenderer : NSObject
     @property (weak) id <myProtocol> myDelegate;
@end

@protocol myProtocol <NSObject>
     - (void)sendToMyViewController : (NSMutableArray *) objects;
@end

In myRenderer.mm (C++ / objective-c code):

@implementation myRenderer

@synthesize myDelegate;
.
.
.
-(void) sendObjects  
{
    [myDelegate sendToMyViewController : objects];

    Problem:
    myDelegate is always <nil>; = (id) 0x0

}
.
.
.
@end

in myViewController.h:

@interface myViewController : UIViewController <myProtocol>
.
.
.
@end

in myViewController.m:

@implementation myViewController
.
.
.
- (void)viewDidLoad
{
    renderer = [[MyRenderer alloc] init];   
    renderer.myDelegate = self;
}
.
.
.

-(void) sendToMyViewController : (NSMutableArray *) objects
{
    // do something with objects

    Problem:
    this method is never called !!!
}
.
.
.
@end

Problem: myDelegate is always nil; equal to (id) 0x0 and sendToMyViewController never called.

Thank you for any help.


Solution

  • Solved!

    @CRD thank you for the hint!

    There was indeed multiple MyRender instances, reducing them to a single instance solved the problem.