我能想到的任何问题都被签出了,但委托方法仍然不会触发。我在Socket.h中声明了一个名为SocketDelegate的协议
@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中,代理收到发送/接收文件的成功/错误通知:
@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];
}
@endViewController.h符合委托:
@interface ViewController : UIViewController <SocketDelegate>在ViewController.m中,我通过一个将套接字和ViewController链接在一起的NetController类来设置委托。我实现了委托方法:
@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之外的其他地方设置代理,但没有效果。当然,我没有编译器错误,也没有运行时错误...我的代码出了什么问题?
发布于 2011-04-28 02:13:28
您确定调用了-viewDidLoad吗?你的ViewController类没有继承任何东西,我想你应该这样做:
@interface ViewController : UIViewController <SocketDelegate>确保调用了-viewDidLoad,如果没有调用,则可能尚未在NIB文件中挂接或以编程方式创建它。下一步,确保套接字函数正在尝试调用委托函数。另外,Socket类也不继承任何东西,我不知道您是如何在netController中构造套接字的,但我不得不更改为
@interface Socket : NSObject为了能够构造一个对象。确保netController中的socket对象构造正确。请记住,如果它们为空,则当您尝试向它们发送消息时,不会给出任何警告。
解决了所有这些问题,这对我很有效。
发布于 2011-04-28 04:46:20
我的猜测是套接字本身(它是NetController对象的一部分)没有正确初始化,或者在触发委托调用之前被释放。如何初始化作为NetController对象一部分的套接字?
https://stackoverflow.com/questions/5806758
复制相似问题