0

如果您需要进一步的解释或详细信息,我会尽我所能解释,请发表评论..

我在我的计算机上运行了一个本地 python 服务器,它根据请求向客户端提供消息。

我有一个 MAC 客户端应用程序,其中有 3 个类 -

  1. Chatlogic 类——该类初始化连接并发送和接收来自服务器的消息。

  2. 登录类 - 这维护用户对应用程序的登录,在这个类中我有一个 Chatlogic 类的实例,我可以通过这样的 chatlogic 类的对象发送消息 [chatLogicObject sendmessage: something ]

我的问题是这个=当我收到它时,它来自 chatLogic 类实例而不是 LoginClass 所以我在登录类中有一个名为 -(void)messageReceived 的方法,它应该覆盖 chatLogic 类中的相同方法(但这不会工作)。我如何在 Loginclass 中接收方法?

为了避免混淆,我添加了我的聊天逻辑类

#import <Foundation/Foundation.h>

@interface chatLogic : NSObject <NSStreamDelegate>

@property (copy)NSOutputStream *outputStream;
@property (copy)NSInputStream *inputStream;
@property (copy)NSString *streamStatus;

-(void)initNetworkCommunications;
-(void)sendMessage:(id)sender;
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)streamEvent;
-(void)messageReceived:(NSString*)message;  //i want this method to be used in some other classes


@end

实现文件如下

导入“chatLogic.h”

@实现聊天逻辑

@synthesize 输入流;@synthesize 输出流;@synthesize 流状态;

-(id)初始化{

if (self == [super init]) {



}
return self;

}

-(void)initNetworkCommunications{

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.22", 80, &readStream, &writeStream);

inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];    

}

-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)streamEvent{

switch (streamEvent) {
    case NSStreamEventOpenCompleted:
        NSLog(@"Stream opened");
        streamStatus = [[NSString alloc]initWithFormat:@"Stream opened"];
        break;
    case NSStreamEventHasBytesAvailable:
        if (aStream == inputStream) {

            uint8_t buffer[1024];
            int len;

            while ([inputStream hasBytesAvailable]) {

                len = (int)[inputStream read:buffer maxLength:sizeof(buffer)];
                if (len > 0) {
                    NSString *output = [[NSString alloc]initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                    if (nil != output) {
                        NSLog(@"server said: %@",output);
                        [self messageReceived:output];
                    }
                }
            }
        }
    case NSStreamEventErrorOccurred:
    {
        NSLog(@"cannot connect to the host!");
        break;

    }

    case NSStreamEventEndEncountered:
    {
        break;
    }

    default:
    {   
        NSLog(@"unknown event!!");
    }
}

}

-(void)sendMessage:(id)sender{

}

-(void)messageReceived:(NSString*)message{

NSLog(@"the received message in chat logic class");

}

-(无效)dealloc{

[inputStream close];
[outputStream close];
[inputStream release];
[outputStream release];
[super dealloc];

}

@结尾

4

2 回答 2

2

我认为您希望在多个班级实例中收到相同的消息。最好的方法是使用 NSNotificationCentre。一旦在一个班级中收到消息,您就可以发布一个通知,然后所有注册该通知的听众都会听到该通知。e

委派也很好,但通常在您确定谁将成为听众时使用(我不确定您的情况发生了什么。)。希望这会有所帮助。

更新:

是的,您也可以发送一个包含一些信息的对象。您可以使用此链接https://stackoverflow.com/a/4127535/919545查看如何发布带有对象的通知

于 2012-06-09T14:13:52.753 回答
1

如果我正确理解了所有内容,那么您在这里谈论的是委托。您应该使用 ( ) 之类的方法定义 ChatLogicDelegate 协议,-(void)didReceiveMessage:(NSString*)message向 ChatLogic 类添加委托属性,并在实例化它时将 loginClassObject 设置为chatLogicObject委托。在 ChatLogic 类中,在需要时调用,loginClassObject将获取消息。[delegate didReceiveMessage:json]

Apple 关于委托的文档

于 2012-06-09T13:45:56.807 回答