c++ - Objective-C 协议(protocol)委托(delegate)不工作

标签 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 从未调用过。

感谢您的帮助。

最佳答案

解决了!

@CRD 谢谢你的提示!

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

关于c++ - Objective-C 协议(protocol)委托(delegate)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47991164/

相关文章:

c++ - 海量数据分析

ios - 可选闭包总是会逃逸吗?我们应该使用[weak self]还是[unowned self]呢?

ios - 当我的应用程序进入后台时,我可以在 dispatch_async 中加入一个循环吗?

objective-c - 在 NSView 上使用 NSPoint 鼠标跟踪

c++ - 如何显示我的队列和堆栈?

c++ - boost::filesystem::path 在 linux 机器上接受 wstring

c++ - 以数字结尾的字符串的正则表达式

iOS 8 : Storage Provider extension allow for importing photos? 是否允许将照片插入到 iMessage 中?

iOS:删除的取消按钮在使用搜索 Controller 后重新出现

objective-c - 如何在 OS X 中的两个应用程序之间共享 NSUserDefaults