Cocoa touch 未激发委托方法
我能想到的任何问题都已检出,但委托方法仍然不会激发。我在Socket.h中声明了一个名为SocketDelegate的协议: ViewController.h符合委托:Cocoa touch 未激发委托方法,cocoa-touch,delegates,Cocoa Touch,Delegates,我能想到的任何问题都已检出,但委托方法仍然不会激发。我在Socket.h中声明了一个名为SocketDelegate的协议: ViewController.h符合委托: @interface ViewController : UIViewController <SocketDelegate> 此外,我还尝试在ViewController.m内的viewDidLoad之外的其他位置设置代理,但没有效果。当然,我没有编译器错误,也没有运行时错误。。。我的代码出了什么问题?您确定调用了-
@interface ViewController : UIViewController <SocketDelegate>
此外,我还尝试在ViewController.m内的viewDidLoad之外的其他位置设置代理,但没有效果。当然,我没有编译器错误,也没有运行时错误。。。我的代码出了什么问题?您确定调用了-viewDidLoad吗?您的ViewController类没有继承任何内容,我认为您需要执行以下操作:
@interface ViewController : UIViewController <SocketDelegate>
为了能够构造一个对象。确保netController中的套接字对象构造正确。请记住,如果它们为零,则在您尝试向它们发送消息时不会发出警告
解决了所有这些问题,这对我来说很有效。我猜测,作为NetController对象一部分的套接字本身没有正确初始化,或者在触发委托调用之前没有释放。如何初始化作为NetController对象一部分的套接字?我试图尽可能简短地发布,但我显然遗漏了很多信息!我为在这里浪费你的时间感到内疚,对不起!事实上,你提到的每件事我都检查了很多次,但我不知道哪里出了错。我找不到任何未初始化的对象,但我想这可能是查找错误的最明显的地方。你同意吗,或者你能想到这里发生的其他事情吗?我非常确定委托的实现和使用设置正确……您可能已经这样做了,但是在-stopSendWithStatus中设置一个断点并再次检查self.delegate,然后进行一步操作以查看到底发生了什么。如果委托不是nil,则它将进入函数或投诉。当我在ViewController类viewDidLoad方法中初始化netController对象时,套接字对象被初始化。我检查过了,他们看起来还不错。。。
@interface ViewController : UIViewController <SocketDelegate>
@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
@interface ViewController : UIViewController <SocketDelegate>
@interface Socket : NSObject