简体   繁体   English

如何在iPhone中多次调用的方法中仅推一次视图

[英]how can i push a view only once with in a method calling multiple times in iphone

 - (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]; 
}

The above delegate method called for every incoming message.When it called, it goes to a new UIViewController.Here my problem is a view pushed multiple tinmes,so error will be occered.how can i fix this error in iphone 上面的委托方法针对每条传入的消息进行调用。调用时,它会转到一个新的UIViewController。这里我的问题是视图推送了多个文本,因此会发生错误。我该如何在iPhone中修复此错误

Add this snippet before pushing the view controller 在推送视图控制器之前添加此代码段

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]; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iPhone SDK中一次播放多种声音? - How can I play multiple sounds at once in the iPhone SDK? 如何在iPhone的相同方法上加载不同的视图 - How can i load a different view on same method in iphone 如何在不调用initWithNibName的情况下推送适当的XIB(Ipad或IPhone) - how can i push appropriate XIB (Ipad or IPhone) without calling initWithNibName Titanium“addEventListener”被多次调用,但我只需要它一次 - Titanium “addEventListener” is called multiple times but I only need it once iPhone,我如何向mainWindow添加一次显示一次的视图,但没有标签栏按钮? - iPhone, how can I add a first view, show once, to a mainWindow but not have a tab bar button? 如何在ios中多次停止调用didUpdateLocations()的方法 - How to stop multiple times method calling of didUpdateLocations() in ios 如何在iPhone中一次性向多个设备发送推送通知? - How can I send push notification to multiple devices in one go in iPhone? 在iPhone上第二次推送后仅显示视图 - Only showing View after second Push on iPhone 如何在iPhone的视图中将分组表下推? - How do I push grouped tables down in the view on the Iphone? 方法在一秒钟内被多次调用,但只应在很小的时间内运行一次 - Method gets called multiple times in a second, but it should only be run once in a small time span
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM