来自iPhone中另一个类的委托的访问值

时间:2011-08-09 11:16:06

标签: iphone

我在iphone中做网络,我能够连接到特定的服务器发送和接收数据。 我的问题是如何在另一个类中访问收到的数据:

例如: 在下面给出的代码中,默认委托流将来自特定服务器的接收数据显示为字符串输出,如何从另一个类访问它?

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

NSLog(@"stream event %i", streamEvent);

switch (streamEvent) {

    case NSStreamEventOpenCompleted:
        NSLog(@"Stream opened");
        break;
    case NSStreamEventHasBytesAvailable:

        if (theStream == inputStream) {

            uint8_t buffer[1024];
            int len;

            while ([inputStream hasBytesAvailable]) {
                len = [inputStream read:buffer maxLength:sizeof(buffer)];
                if (len > 0) {

                    output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                    if (nil != output) {

                        NSLog(@"server said: %@", output);
                        [self messageReceived:output];
                    }
                }
            }
        }
        break;


    case NSStreamEventErrorOccurred:

        NSLog(@"Can not connect to the host!");
        break;

    case NSStreamEventEndEncountered:

        [theStream close];
        [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [theStream release];
        theStream = nil;

        break;
    default:
        NSLog(@"Unknown event");
}

}

从另一节课中,我做了以下事情:

SEL mySelector = @selector(stream:handleEvent:);
[NSTimer scheduledTimerWithTimeInterval:30.0 target:ISIS selector:mySelector       
userInfo:nil repeats:YES];
 NSLog(@"Output: %@",ISIS.output);

但是我为ISIS.output获取了null。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您应该将数据从handleEvent传递到另一个类,而不是从其他类调用handleEvent。您可以将另一个类设为委托,并在handleEvent -

中执行以下操作
[self.delegate actOnEvent:data]; //where data is whatever you want to pass & actOnEvent is the name of your method