2011-12-19 57 views
26

我有一個UITabBarController,當初始運行時,我想覆蓋登錄視圖控制器但收到錯誤。UITabBarController的開始/結束外觀轉換的不平衡調用

對於< UITabBarController:0x863ae00>開始/結束外觀轉換的不平衡調用。

以下是代碼。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 

    // Override point for customization after application launch. 

    UIViewController *lessonVC = [[[LessonViewController alloc] initWithNibName:@"LessonViewController" bundle:nil] autorelease]; 

    UIViewController *programVC = [[[ProgramViewController alloc] initWithNibName:@"ProgramViewController" bundle:nil] autorelease]; 

    UIViewController *flashcardVC = [[[FlashCardViewController alloc] initWithNibName:@"FlashCardViewController" bundle:nil] autorelease]; 

    UIViewController *moreVC = [[[MoreViewController alloc] initWithNibName:@"MoreViewController" bundle:nil] autorelease]; 

    UINavigationController *lessonNVC = [[[UINavigationController alloc] initWithRootViewController:lessonVC] autorelease]; 

    UINavigationController *programNVC = [[[UINavigationController alloc] initWithRootViewController:programVC] autorelease]; 

    UINavigationController *flashcardNVC = [[[UINavigationController alloc] initWithRootViewController:flashcardVC] autorelease]; 

    UINavigationController *moreNVC = [[[UINavigationController alloc] initWithRootViewController:moreVC] autorelease]; 

    self.tabBarController = [[[UITabBarController alloc] init/*WithNibName:nil bundle:nil*/] autorelease]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:lessonNVC, programNVC, flashcardNVC, moreNVC, nil]; 
    self.tabBarController.selectedIndex = 0; 
    self.window.rootViewController = self.tabBarController; 

    [self.window makeKeyAndVisible]; 

    if (![[ZYHttpRequest sharedRequest] userID]) 
    { 
     // should register or login firstly 
     LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" 
                      bundle:nil]; 
     loginVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
     [self.tabBarController presentModalViewController:loginVC animated:YES]; 
     ZY_SAFE_RELEASE(loginVC); 
    } 

    return YES; 
} 

任何人都可以幫助我嗎?提前致謝!

+0

另外,我檢查了這個[http://stackoverflow.com/q/7886096/527539]。但沒有運氣。 – ZYiOS 2011-12-19 15:25:30

回答

76

你需要等待目前的模態視圖控制器,直到下一次運行循環。我結束了使用的塊(使事情變得更加簡單)爲計劃下次運行循環演示:

更新:
正如下面馬克·埃默裏,只是一個簡單的dispatch_async作品中提到,有沒有必要定時器:

dispatch_async(dispatch_get_main_queue(), ^(void){  
    [self.container presentModalViewController:nc animated:YES]; 
}); 

/* Present next run loop. Prevents "unbalanced VC display" warnings. */ 
double delayInSeconds = 0.1; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    [self.container presentModalViewController:nc animated:YES]; 
}); 
+4

這裏不需要計時器,至少在我的情況下(你的回答解決了我的警告)。只是做 'dispatch_async(dispatch_get_main_queue(),^(無效){ [self.container presentModalViewController:NC動畫:YES]; });' 這是更簡單和更哈克。 – 2013-09-18 15:27:42

+0

我完全同意Mark的意思,只是使用'dispatch_async(dispatch_get_main_queue(),{code block}) – Dean 2014-07-24 23:53:38

+7

'dispatch_async()'在我的iOS8上不能工作,但dispatch_after()工作。它的缺點是,我看到rootViewController一會兒(因爲0.1f的延遲)。 – SoftDesigner 2014-09-22 11:41:00

10

我懷疑問題在於,在標籤欄完成加載之前,您正試圖撥打presentModalViewController:。嘗試最後的邏輯移動到下一個事件循環:

[self.window makeKeyAndVisible]; 
    [self performSelector:(handleLogin) withObject:nil afterDelay:0]; 
} 

- (void)handleLogin 
{ 
    if (![[ZYHttpRequest sharedRequest] userID]) 
    { 
     // should register or login firstly 
     LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" 
                      bundle:nil]; 
     loginVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
     [self.tabBarController presentModalViewController:loginVC animated:YES]; 
     ZY_SAFE_RELEASE(loginVC); 
    } 
} 
+0

嗨,羅布,謝謝你的回答!它解決了我的問題! – ZYiOS 2011-12-28 14:43:48

+0

[self performSelector:(handleLogin)withObject:nil afterDelay:0.1];在我的iPod 4G上工作,如果延遲時間爲0,只能在模擬器上工作,但在設備中獲得相同的警告。 – ZYiOS 2011-12-28 15:08:07

+0

謝謝,謝謝,謝謝。選定的答案有相同的想法,但更復雜,這對我來說很好。 – 2013-07-20 21:45:51

5
[self.tabBarController presentModalViewController:loginVC animated:**NO**]; 
+0

這適用於我--- +1 – 2012-11-21 23:32:17

+0

很棒! +1 – Nanego 2013-08-03 03:59:56

+4

此變通辦法不是解決方案。 – Karsten 2014-07-18 07:33:51

2

我有一個類似的問題,當t在主視圖的viewWillAppear中前往ModalViewController(我的歡迎屏幕)。僅通過將模態VC調用移動到viewDidAppear即可解決。

0
[self performSelector:@selector(modaltheView) withObject:self afterDelay:0.1]; 
-(void)modaltheView 
{ 
    [self.container presentModalViewController:nc animated:YES]; 
} 
相關問題