0
 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
       ChatViewController *chatView;
       if(contactView==nil)
       {                
       chatView=[[ChatViewController alloc] initWithNibName:@"ChatViewController" bundle:nil];
        }   
       [self.navigationController pushViewController:chatView animated:YES];
       [messageDelegate newMessageReceived:m]; 
}

上面的委托方法为每个传入的消息调用。当它调用时,它会转到一个新的 UIViewController。我的问题是一个视图推送了多个 tinmes,所以会发生错误。我该如何在 iphone 中修复这个错误

4

1 回答 1

3

在推送视图控制器之前添加此代码段

BOOL viewControllerAlreadyPushed = NO;
for (UIViewController *controller in self.navigationController.viewControllers) {
    if ([controller isKindOfClass:[ChatViewController class]]) {
        viewControllerAlreadyPushed = YES;
    }
}

if(!viewControllerAlreadyPushed) //if not pushed, push it
{
    ChatViewController *chatView;
    if(contactView==nil)
    {                
        chatView=[[ChatViewController alloc] initWithNibName:@"ChatViewController" bundle:nil];
    }   
    [self.navigationController pushViewController:chatView animated:YES];
    [messageDelegate newMessageReceived:m]; 
}
于 2012-06-30T13:26:04.883 回答