正确定义委托从AppDelegate调用函数

时间:2013-12-07 15:51:45

标签: ios iphone objective-c

我有一个.h文件

@protocol SMMessageDelegate

- (void)newMessageReceived:(NSDictionary *)messageContent;

@end

在我的AppDelegate.h中

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    __weak NSObject <SMMessageDelegate> *messageDelegate;
}

@property (nonatomic, assign) id  _messageDelegate;

在AppDelegate.m中我正在调用

[messageDelegate newMessageReceived:m];

在某个地方。

在我的ViewController中

    #import "SMMessageDelegate.h"

    @interface ChatController : UIViewController <SMMessageDelegate>

    ChatController.m

    -(void) viewDidLoad  
   {
      AppDelegate *delegate =  (AppDelegate *) [[UIApplication sharedApplication] delegate];
        delegate._messageDelegate = self;
   }

    - (void)newMessageReceived:(NSDictionary *)messageContent
    {

    }

然而,当我的代码转移到AppDelegate

[messageDelegate newMessageReceived:m];

它从不调用上面的ViewController委托函数。在正确定义代表方面我错了吗?

1 个答案:

答案 0 :(得分:2)

替换它:

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    __weak NSObject <SMMessageDelegate> *messageDelegate;
}

@property (nonatomic, assign) id  _messageDelegate;

使用:

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, assign) id<SMMessageDelegate>  _messageDelegate;