TabBarController rotation problem with popViewControllerAnimated and selectedIndex

Posted by rjobidon on Stack Overflow See other posts from Stack Overflow or by rjobidon
Published on 2010-04-23T15:31:47Z Indexed on 2010/04/23 15:33 UTC
Read the original article Hit count: 472

Hi!

This is a very important auto rotate issue and easy to reproduce.

My application has a UITabBarController. Each tab is a UINavigationController. Auto rotation is handled with normal calls to shouldAutorotateToInterfaceOrientation and didRotateFromInterfaceOrientation.

The interface rotates normally until I call UIViewController.popViewControllerAnimated and change UITabBarController.selectedIndex.

Steps to reproduce:

  1. Create a demo Tab Bar Application.
  2. Add the following code to the App Delegate .h file:
    
    #import <UIKit/UIKit.h>
    @interface TestRotation2AppDelegate : NSObject { UIWindow *window; UITabBarController *tabBarController; }
    @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
    • (void)doAction;
      @end
      // Redefine the interface to cach rotation messages @interface UITabBarController (TestRotation1AppDelegate)
      @end
  3. Add the following code to the App Delegate .m file:

#import "TestRotation2AppDelegate.h"
@implementation TestRotation2AppDelegate
@synthesize window;
@synthesize tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)dealloc {
    [tabBarController release];
    [window release];
    [super dealloc];
}

@end

@implementation UITabBarController (TestRotation1AppDelegate) 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Add a third tab and push a view 
    UIViewController *view1 = [[UIViewController alloc] init];
    view1.title = @"Third";
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:view1];
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObjectsFromArray:self.viewControllers];
    [array addObject:nav];
    self.viewControllers = array;
    // Push view2 inside the third tab
    UIViewController *view2 = [[UIViewController alloc] init];
    [nav pushViewController:view2 animated:YES];
    // Create a button to pop view2
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 50, 220, 38);
    [button setTitle:@"Pop this view" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(doAction) forControlEvents:UIControlEventTouchUpInside];
    [view2.view addSubview:button];
}

- (void) doAction {
    // ROTATION PROBLEM BEGINS HERE
    // Remove one line of code and the problem doesn't occur.
    [self.selectedViewController popViewControllerAnimated:YES];
    self.selectedIndex = 0;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

@end

The interface auto rotates normally until you tap the button on tab #3.

Your help will be geatly appreciated!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uitabbarcontroller