访问从一个类到另一个类的方法,从服务器接收消息

时间:2012-06-09 13:31:48

标签: objective-c xcode macos

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

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

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

  1. Chatlogic类 - 此类初始化连接并从服务器发送和接收消息。

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

  3. 我的问题是这个=当我收到它时,它来自chatLogic类实例,而不是在LoginClass中,所以我在登录类中有一个名为 - (void)messageReceived的方法应该覆盖chatLogic类中的相同方法(但是这不起作用)。 我怎样才能在Loginclass中收到该方法?

    为了避免混淆,我添加了我的chatlogic类

    #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”

    @implementation chatLogic

    @synthesize inputStream; @synthesize outputStream; @synthesize streamStatus;

    - (ID)INIT {

    if (self == [super init]) {
    
    
    
    }
    return self;
    

    }

    - (无效)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!!");
        }
    }
    

    }

    - (无效)的sendMessage:(ID)发送方{

    }

    - (无效)的messageReceived:(* NSString的)消息{

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

    }

    - (无效)的dealloc {

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

    }

    @end

2 个答案:

答案 0 :(得分:2)

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

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

<强>更新

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

答案 1 :(得分:1)

如果我理解了一切,你在这里谈论代表团。 您应该使用类似(-(void)didReceiveMessage:(NSString*)message)的方法定义ChatLogicDelegate协议,向ChatLogic类添加委托属性,并在实例化时将 loginClassObject 设置为 chatLogicObject的委托。在ChatLogic类中,每当必要时调用[delegate didReceiveMessage:json]并且 loginClassObject 将获取消息。

Apple's documentation on delegation