objective-c - 第二次调用 loadView 时,iOS 5 不会自动旋转 View

标签 objective-c ios uiviewcontroller ios5 rotation

当我在横向模式下运行应用程序时,第二次加载 View Controller 的 View 并调用 loadView 时,iOS 5 不会将 Root View Controller 的 View 旋转远离纵向,也不会调用 View Controller 的 willRotate/willAnimateRotation/didRotate 方法。

如上所述,这种情况仅在 View 被卸载并且第二次调用 loadView 后发生。在我的应用程序中,有时我会在将 View Controller 设置为 Root View Controller 之前以编程方式卸载它。但是,当 View Controller 的 View 由于内存不足警告而自动卸载时,也会发生这种情况。

这只发生在 iOS 5 上,而不是 iOS 4.x 上。在 iOS 模拟器中很容易重现,这里是重现此问题的独立测试应用程序的链接:

http://www.mediafire.com/file/7ob5xw5ym02pasx/ViewControllerTest.zip

要查看该问题,只需编译应用程序并使用 iOS 5.0 模拟器以横向模式启动它,然后点击“A”,然后点击“返回”,您就会看到如何不发生旋转。

有什么想法吗?这与我设置 window.rootViewController 的方式有关吗?

最佳答案

我调试了一下,4.3和5.0之间似乎有变化。在 4.3 中 UIKit 调用 [UIWindow addRootViewController:],在 5.0 中调用 [UIWindow setRootViewController:]。所以也许有些东西发生了我们看不到的变化......

我建议通过使用[newController setupStuff]来解决这个问题(因此将 View 设置为零),但是相当压倒一切

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // do setup stuff here, reset content etc etc.
}

并在(已加载的) View 实际出现在屏幕上之前重置它。

其他信息:

a) 您不需要将 View 设置为零。系统会为您做到这一点。当您下次访问该 View Controller 的 view 属性时,系统会调用 loadView 重新创建 View :

If you access this property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view. The default loadView method attempts to load the view from the nib file associated with the view controller (if any).

b) 这一切都归结为 iOS 5 中有关窗口的 rootViewController 属性的行为。在 iOS 5 中,这两个等效:

 self.window.rootViewController = newController;

对比

 [self.window.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
 [self.window addSubview:newController.view];

前者完全忽略方向,后者则不然。

让事情变得更复杂的是,如果不覆盖 loadView,而是让系统自动创建一个空的 UIView 并仅修改 中的该 View >viewDidLoad,内存警告不会自动将 View 设置为 nil 并释放它 - 文档:

The default implementation releases the view only if it determines that it is safe to do so.

魔法。取决于是否重写该方法。我无法以任何方式真正测试这一点。

情况 1 - 重写 loadView:
触发内存警告同时调用 didReceiveMemoryWarningviewDidUnload

情况 2 - 没有 loadView:
在模拟器中触发内存警告调用didReceiveMemoryWarning,但不调用viewDidUnload

我想说,手动添加 View ...

干杯。

关于objective-c - 第二次调用 loadView 时,iOS 5 不会自动旋转 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8568209/

相关文章:

ios - 推送导航动画而不重叠 UIViewControllers

iphone - 用粉笔效果画线( cocoa / cocoa 触感)?

iphone - .nib 文件的多个版本

ios - 如何快速测试 XCTest 中没有静态文本的 Webview 加载与否

ios - ARKit - SCNNode 连续运动

iOS 方向改变,但页面仍然垂直

objective-c - 在 XCode 5 中显示该特定年份的月份列表

objective-c - 如何约束NSSplitView?

ios - Swift 中带有圆角半径的 Collection View

objective-c - UISplitViewController EXC_BAD_ACCESS 使用 ARC 旋转