Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unbalanced calls to begin/end appearance transitions for UITabBarController

I have an UITabBarController, when initial run, I want to overlay a login view controller but received error.

Unbalanced calls to begin/end appearance transitions for < UITabBarController: 0x863ae00 >.

Below is the code.

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

Anyone who can help me? Thanks in advance!

like image 313
ZYiOS Avatar asked Dec 19 '11 15:12

ZYiOS


4 Answers

You need to wait to present the modal view controller until the next run loop. I ended up using a block (to make things more simple) to schedule the presentation for the next run loop:

Update:
As mentioned by Mark Amery below, just a simple dispatch_async works, there's no need for a timer:

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];
});
like image 155
Maurizio Avatar answered Nov 17 '22 14:11

Maurizio


I suspect the problem is that you're trying to call presentModalViewController: before the tab bar is done loading. Try moving the final logic onto the next event loop:

  [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);
    }
}
like image 10
Rob Napier Avatar answered Nov 17 '22 15:11

Rob Napier


[self.tabBarController presentModalViewController:loginVC animated:**NO**];
like image 5
Rio V Avatar answered Nov 17 '22 15:11

Rio V


I had a similar problem when tried to presentModalViewController (my welcome screen) in main view's viewWillAppear. Is was solved just by moving the modal VC call to viewDidAppear.

like image 2
Arseniy Avatar answered Nov 17 '22 16:11

Arseniy