Objective-C协议委托不起作用

时间:2017-12-27 11:13:32

标签: c++ ios objective-c delegates protocols

我想从myRenderer

将一组对象发送回myViewController

在myRenderer.h中:

@protocol myProtocol;

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

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

在myRenderer.mm(C ++ / objective-c代码)中:

@implementation myRenderer

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

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

}
.
.
.
@end
在myViewController.h中

@interface myViewController : UIViewController <myProtocol>
.
.
.
@end
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

问题: myDelegate总是零;等于(id)0x0和 sendToMyViewController从未调用过。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

解决!

@CRD谢谢你的提示!

确实存在多个MyRender实例,将它们缩减为单个实例解决了问题。