这是一个非常重要的自动旋转问题并且很容易重现。
我的应用程序有一个 UITabBarController。每个选项卡都是一个 UINavigationController。自动旋转通过对 shouldAutorotateToInterfaceOrientation 和 didRotateFromInterfaceOrientation 的正常调用来处理。
界面正常旋转,直到我调用 UIViewController.popViewControllerAnimated 并更改 UITabBarController.selectedIndex。
重现步骤:
- 创建一个演示标签栏应用程序。
- 将以下代码添加到 App Delegate .h 文件中:
#import <UIKit/UIKit.h>
@interface TestRotationAppDelegate : NSObject { UIWindow *window; UITabBarController *tabBarController; }
@property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
// Redefine the interface to cach rotation messages @interface UITabBarController (TestRotation1AppDelegate)
@end - 将以下代码添加到 App Delegate .m 文件:
#import "TestRotationAppDelegate.h"
@implementation TestRotationAppDelegate @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] autorelease]; view1.title = @"Third"; UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:view1] autorelease]; 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] autorelease]; [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
界面自动旋转正常,直到您点击标签 #3 上的按钮。
您的帮助将不胜感激!