1

我写了一个聊天应用程序,它有几个不同的视图控制器。

我已经实现了一个 Singelton 来监听来自服务器的新消息。当我收到一条新消息时,我想在另一个 viewController( TabFirstViewController) 中更新正确的 TableView,调用该 viewController 中的表visitorsTableView

这是我当前的单例实现:

#import "ChatDataController.h"
#import "TabFirstViewController.h"

@implementation ChatDataController
{
    ChatDataController * anotherSingles;
}

@synthesize enString;
@synthesize enInt;
@synthesize messages;


+ (ChatDataController *)sharedInstance
{
    static dispatch_once_t once;
    static ChatDataController *chatDataController;
    dispatch_once(&once, ^ { chatDataController = [[ChatDataController alloc] init];});
    return chatDataController;
}


- (id)init
{
    self = [super init];

    if (self) {
        messages = [[NSMutableArray alloc] init];
    }

    return self;
}

// Open connection to server
- (void)initNetworkCommunication {
    isConnected = TRUE;
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 8080, &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 *)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) {

                        NSString *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!");
            isConnected = 0;
            break;

        case NSStreamEventEndEncountered:

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

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

}

// I want to update the table from this method...
- (void) messageReceived:(NSString *)message {

    [self.messages addObject:message];

    /**
    * Trying to update the table..
    */
    TabFirstViewController *controller = [[TabFirstViewController alloc] init];

    NSString *s = (NSString *) [NSIndexPath indexPathForRow:messages.count-1 inSection:0];


    NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:messages.count-1
                                                   inSection:0];
    [controller.visitorsTableView scrollToRowAtIndexPath:topIndexPath
                      atScrollPosition:UITableViewScrollPositionMiddle
                              animated:YES];

    [controller.visitorsTableView reloadData];
}

@end

我应该如何更新我的visitorsTableView表?当我收到来自服务器的新消息时,我想保持更新。

应该说我有更多的viewControllers,每个viewControllers都包含一个tableView,所以我也希望能够更新它们。

4

2 回答 2

0

每次收到消息时都不能初始化表格视图。有两种方法, 1. 使用委托模式,当您需要将调用委托给单个处理程序时首选 2. 使用本地通知 当您有多个处理程序到同一个通知时首选。(我会为你的聊天应用推荐这个)

于 2013-08-01T10:52:37.430 回答
0

我建议您使用通知在收到新消息时通知您。通知回调可用于重新加载表视图。

于 2013-08-01T10:54:22.080 回答