2011-09-11 35 views
4

我收到以下錯誤,每當我嘗試切換視圖這樣的:不支持多次推送視圖控制器?

-(void)changeView1ToView4 { 
    [self.navigationController1 pushViewController:view4 animated:YES]; 
} 

當應用程序首次加載和用戶直接進入這一觀點不會出現這種情況。只有當我轉到其他視圖之一,回到主菜單,然後嘗試轉到此視圖時,纔會發生這種崩潰。

此外,如果你不確定,我正在使用UINavigationController。另外這個代碼是在應用程序的委託,我從其中有一個父視圖,視圖控制器,所以我使用。參照來稱呼它稱之爲:

[self.reference changeView1ToView4]; 

是否有解決這個問題的任何實際的方式?

謝謝!

EDIT1:

[self.navigationController1 pushViewController:view4 animated:NO]; 
[self.navigationController1 pushViewController:view4 animated:YES]; 

I tried that and got this crash message in the console: 

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported (<View2: 0x18d540>)' 
+0

我真的不知道你的問題在談論,但沒錯,這是真的,你*不能*推了同樣的觀點控制器超過once- UINavigationController將引發異常。你無能爲力。 – FeifanZ

回答

8

當按下2次壓入堆棧,嘗試撥打:

[self.navigationController1 pushViewController:view4 animated:YES]; 

[self.navigationController1 pushViewController://secondviewcontrollername// animated:NO]; 

如果你試圖把一個以上的視圖,將animated:字段設置爲YES,那麼您會混淆堆棧。一次只能動畫一個視圖。

+0

代碼所在的位置是否重要? –

+0

是的。將視圖控制器先放到最上面(顯然是非動畫的),然後把動畫放在最下面。堆棧將在頂部調用視圖控制器,然後底部和底部的視圖控制器應該是您看到的視圖。告訴我,如果不是。 – CodaFi

+0

檢查編輯1在我原來的文章 –

4

只是一個FYI,如果你調用setViewControllers:animated:沒有必要調用pushViewController:之後,否則你會得到「不止一次推送相同的視圖控制器實例不支持」崩潰。

0
  @try { 
       [self.navController pushViewController:viewController animated:NO]; 
      } @catch (NSException * e) { 
       NSLog(@"Exception: %@", e); 
       [self.navigationController popToViewController:viewController animated:NO]; 
      } @finally { 
       //NSLog(@"finally"); 
      } 
0

入住前推這個條件

if (![[self.navigationController topViewController] isKindOfClass:[YOURCONTROLLER class]]) 
    [self.navigationController pushViewController:YOURCONTROLLER animated:YES];