从Singleton更新表?

时间:2013-08-01 10:44:55

标签: iphone ios objective-c

我正在编写一个包含多个不同viewControllers的聊天应用。

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

这是我当前的Singleton实现:

#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,每个都包含一个tableView,所以我希望能够在线下更新它们。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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