Quickblox聊天应用:收到的新消息应该触发相应的uitableviewcell

时间:2014-05-13 14:13:08

标签: ios chat quickblox

我目前正在使用Quickblox为后端开发聊天应用。一切正常,除了在didReceiveRemoteNotification中收到新消息时,检查应用程序状态是否处于非活动状态和活动状态。如果处于活动状态,则会触发UIAlertView,并提供查看新消息或忽略的选项。 Ignore会将传入的消息保存到CustomObjects,而view应该触发打开消息发送者的相应uitableviewcell,然后打开发件人的相应聊天视图。

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// the user clicked OK
if (buttonIndex == 0) {
    QBCOCustomObject *object = [QBCOCustomObject customObject];
    object.className = @"Messages"; // your Class name
    // object fields
    [object.fields setObject:message forKey:@"Message_Text"];
    [object.fields setObject:sender forKey:@"Sender_ID"];
    [object.fields setObject:file forKey:@"Audio_Ref"];
    [QBCustomObjects createObject:object delegate:self];

}else if (buttonIndex == 1) {
    //[self prepareToSegue: self.thisUserInfo ];
    // Download rich contentx
    [QBContent TDownloadFileWithBlobID:[file integerValue] delegate:self];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"fireTableCell"
                                                        object:msgArray];
}

}

//在UserView中

-(void) fireTableCell: (NSNotification*) notification {
// takes in an array with the sender and message received
messageIn = [notification.object objectAtIndex:0];
QBUUser *user = [QBUUser user];
user.login = [notification.object objectAtIndex:1];

NSLog(@" * * the sender and message are %@ and %@", user.login, messageIn);

// load the chatview
NSLog(@"  * * * The dict is %@", dict);

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
EmojiViewController *controller = (EmojiViewController *)[storyboard instantiateViewControllerWithIdentifier:@"chatView"];
controller.opponent = user;
//controller.messages = [[NSMutableArray array]init];
[controller.messages addObject:messageIn];
[controller.messagesTableView reloadData];
if (controller.presentingViewController == nil) {
    [self.navigationController pushViewController:controller animated:YES];
}

}

以上所有工作仅在按下聊天视图时按预期工作,它是创建此视图的新实例而不是现有实例,因此看起来消息历史记录已被删除。有人可以告诉我我哪里出错了吗?三江源。

1 个答案:

答案 0 :(得分:1)

方法instantiateViewControllerWithIdentifier:创建一个新的控制器实例。因此,如果您已经拥有所需的实例,那么尝试从storyboard实例化您的控制器您将拥有两个不同的控制器实例。

因此,每个控制器实例都有一条消息。

这里我为方法viewDidLoad中的测试而做:从storyboard获取当前控制器的实例(查看实例的地址):

http://gyazo.com/7adc254fdd168803a1883791b63e9be2