0
這是一個非常重要的自動旋轉問題,並容易重現。UITabBarController旋轉問題與popViewControllerAnimated和selectedIndex(iPhone SDK)
我的應用程序有一個的UITabBarController。每個選項卡是一個UINavigationController。自動旋轉與正常調用shouldAutorotateToInterfaceOrientation和didRotateFromInterfaceOrientation處理。
界面正常旋轉,直到我調用UIViewController.popViewControllerAnimated並更改UITabBarController.selectedIndex。
重現步驟:
- 創建演示標籤欄的應用。
- 下面的代碼添加到應用程序委託.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 - 下面的代碼添加到應用程序委託.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。
你的幫助將geatly感激!