popViewControllerAnimated 和 selectedIndex 的 UITabBarController 旋转问题(iPhone SDK)

发布于 2024-08-30 00:55:06 字数 2643 浏览 7 评论 0原文

这是一个非常重要的自动旋转问题并且易于重现。

我的应用程序有一个 UITabBarController。每个选项卡都是一个 UINavigationController。自动旋转是通过正常调用 shouldAutorotateToInterfaceOrientation 和 didRotateFromInterfaceOrientation 来处理的。

界面正常旋转,直到我调用 UIViewController.popViewControllerAnimated 并更改 UITabBarController.selectedIndex。

重现步骤:

  1. 创建一个演示选项卡栏应用程序。
  2. 将以下代码添加到 App Delegate .h 文件中:
    #import 
    @interface TestRotationAppDelegate : NSObject { UIWindow *窗口; UITabBarController *tabBarController; }
    @property(非原子,保留)IBOutlet UIWindow *window; @property(非原子,保留)IBOutlet UITabBarController *tabBarController;
    @结束
    // 重新定义缓存轮换消息的接口 @interface UITabBarController (TestRotation1AppDelegate)
    @结尾
  3. 将以下代码添加到 App Delegate .m 文件中:
    #import "TestRotationAppDelegate.h"
    @implementation TestRotationAppDelegate @合成窗口; @synthesize tabBarController;
    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [窗口addSubview:tabBarController.view]; [窗口 makeKeyAndVisible]; 返回是; }
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 返回是; }
    -(void)dealloc { [tabBarController 发布]; [窗口释放]; [超级释放]; }
    @结束
    @implementation UITabBarController (TestRotation1AppDelegate)
    -(void)viewDidLoad { [超级viewDidLoad]; // 添加第三个选项卡并推送视图 UIViewController *view1 = [[[UIViewController alloc] init] autorelease]; view1.title = @"第三个"; UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:view1] autorelease]; NSMutableArray *array = [[NSMutableArray alloc] init]; [数组 addObjectsFromArray:self.viewControllers]; [数组addObject:nav]; self.viewControllers = 数组; // 将 view2 推入第三个选项卡 UIViewController *view2 = [[[UIViewController alloc] init] autorelease]; [nav PushViewController:view2动画:是]; // 创建一个按钮来弹出view2 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 按钮.frame = CGRectMake(50, 50, 220, 38); [button setTitle:@"弹出此视图" forState:UIControlStateNormal]; [按钮 addTarget:self 操作:@selector(doAction) forControlEvents:UIControlEventTouchUpInside]; [view2.view addSubview:按钮]; }
    -(无效) doAction { // 旋转问题从这里开始 // 去掉一行代码,问题就不会出现。 [self.selectedViewController popViewControllerAnimated:YES]; self.selectedIndex = 0; }
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 返回是; }
    @结尾

界面会正常自动旋转,直到您点击选项卡 #3 上的按钮。

我们将非常感谢您的帮助!

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 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
  3. Add the following code to the App Delegate .m file:
    #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

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

Your help will be geatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云雾 2024-09-06 00:55:06

iPhone SDK 3.2 解决了这个问题。

对于以前的 SDK,请使用 [self.selectedViewController popViewControllerAnimated:NO]。

iPhone SDK 3.2 solves this issue.

With previous SDK use [self.selectedViewController popViewControllerAnimated:NO].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文