我写了一个聊天应用程序,它有几个不同的视图控制器。
我已经实现了一个 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,所以我也希望能够更新它们。