Iphone 是否从单台更新表?

Iphone 是否从单台更新表?,iphone,ios,objective-c,Iphone,Ios,Objective C,我正在编写一个聊天应用程序,它有几个不同的ViewController 我实现了一个Singleton来监听来自服务器的新消息。当我收到一条新消息时,我想在另一个viewController选项卡FirstViewController中更新正确的TableView。该viewController中的表称为visitorsTableView 以下是我当前的单例实现: #import "ChatDataController.h" #import "TabFirstViewController.h"

我正在编写一个聊天应用程序,它有几个不同的ViewController

我实现了一个Singleton来监听来自服务器的新消息。当我收到一条新消息时,我想在另一个viewController选项卡FirstViewController中更新正确的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表?当我从服务器收到新消息时,我希望保持更新


应该说,我有更多的ViewController,每个都包含一个tableView,因此我希望能够同时更新它们。

您不能在每次收到消息时初始化Table view。 有两种方式,, 1.使用委托模式, 当您需要将调用委托给单个处理程序时,首选此选项 2.使用本地通知
当您有多个处理程序处理同一通知时,首选。我将向您的聊天应用程序推荐此功能

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