首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未激发委托方法

未激发委托方法
EN

Stack Overflow用户
提问于 2011-04-27 23:41:32
回答 2查看 230关注 0票数 0

我能想到的任何问题都被签出了,但委托方法仍然不会触发。我在Socket.h中声明了一个名为SocketDelegate的协议

代码语言:javascript
复制
@protocol SocketDelegate <NSObject>
@optional
- (void)socket:(Socket *)socket handleNewConnection:(NSString *)test;
- (void)socket:(Socket *)socket didSend:(BOOL)didSend;
- (void)socket:(Socket *)socket didReceive:(BOOL)didReceive;
@end

@interface Socket : NSObject {
    id<SocketDelegate> delegate;
}

@property(nonatomic,assign) id<SocketDelegate> delegate;

@end

现在,在Socket.m中,代理收到发送/接收文件的成功/错误通知:

代码语言:javascript
复制
@implementation Socket

/* I checked: both of these methods are called */

- (void)stopSendWithStatus:(NSString *)statusString {
    [self.delegate socket:self didSend:isSent];
}

- (void)stopReceiveWithStatus:(NSString *)statusString {
    [self.delegate socket:self didReceive:isReceived];
}

@end

ViewController.h符合委托:

代码语言:javascript
复制
@interface ViewController : UIViewController <SocketDelegate>

ViewController.m中,我通过一个将套接字和ViewController链接在一起的NetController类来设置委托。我实现了委托方法:

代码语言:javascript
复制
@implementation ViewController

- (void)viewDidLoad {
    /* I checked: this method is called */
    /* Both 'netController' and 'socket' are initialized correctly
    netController = [[NetController alloc] init];
    [[netController socket] setDelegate:self];  
}

@end

@implementation ViewController (SocketDelegate)

- (void)socket:(Socket *)socket didSend:(BOOL)didSend {
    NSLog(@"didSend %@", didSend);        // Nothing happens...
}

- (void)socket:(Socket *)socket didReceive:(BOOL)didReceive {
    NSLog(@"didReceive %@", didReceive);  // Nothing happens...
}

@end

此外,我还尝试在ViewController.m中的viewDidLoad之外的其他地方设置代理,但没有效果。当然,我没有编译器错误,也没有运行时错误...我的代码出了什么问题?

EN

回答 2

Stack Overflow用户

发布于 2011-04-28 02:13:28

您确定调用了-viewDidLoad吗?你的ViewController类没有继承任何东西,我想你应该这样做:

代码语言:javascript
复制
@interface ViewController : UIViewController <SocketDelegate>

确保调用了-viewDidLoad,如果没有调用,则可能尚未在NIB文件中挂接或以编程方式创建它。下一步,确保套接字函数正在尝试调用委托函数。另外,Socket类也不继承任何东西,我不知道您是如何在netController中构造套接字的,但我不得不更改为

代码语言:javascript
复制
@interface Socket : NSObject

为了能够构造一个对象。确保netController中的socket对象构造正确。请记住,如果它们为空,则当您尝试向它们发送消息时,不会给出任何警告。

解决了所有这些问题,这对我很有效。

票数 0
EN

Stack Overflow用户

发布于 2011-04-28 04:46:20

我的猜测是套接字本身(它是NetController对象的一部分)没有正确初始化,或者在触发委托调用之前被释放。如何初始化作为NetController对象一部分的套接字?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5806758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档