如何在iphone中多次调用方法中只推送一次视图

时间:2012-06-30 13:22:22

标签: iphone pushviewcontroller

 - (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,所以错误将是occered.how我可以在iphone中修复此错误

1 个答案:

答案 0 :(得分: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]; 
}