为什么 iOS 不自动旋转由 didReceiveMemoryWarning 释放后从 Nib 加载的视图?

发布于 2024-10-11 05:52:13 字数 557 浏览 9 评论 0原文

我的 iPad 应用大量使用自动旋转。这太棒了。但是,我注意到,如果 didReceiveMemoryWarning 的默认实现释放了隐藏视图(如所述此处),当从笔尖重新加载视图并且我碰巧处于横向时,它会加载它的肖像。这会对界面造成严重破坏,直到我手动旋转 iPad 并强制它转到正确的方向。

我曾假设 iOS 会以当前方向加载视图;但事实并非如此。这就是应用程序启动时所做的事情。但它不,不是在被 didReceiveMemoryWarning 卸载后。为什么不呢?我怎样才能让它做到这一点?

My iPad app makes heavy use of autorotation. This is great. However, I've noticed that if a hidden view is released by the default implementation of didReceiveMemoryWarning (as described here), when the view is re-loaded from the nib and I happen to be in landscape, it loads it in portrait. This wreaks havoc with the interface until I rotate the iPad manually and force it to go to the proper orientation.

I had assumed that iOS would load the view in the current orientation; that's what it does when the app launches. But it no, not after being unloaded by didReceiveMemoryWarning. Why not? And how can I get it to do that?

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

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

发布评论

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

评论(2

離人涙 2024-10-18 05:52:13

通过 dbarker 的指针确定的答案是视图控制器的旋转方法,包括 -willRotateToInterfaceOrientation:duration :-willAnimateRotationToInterfaceOrientation:duration:,在 didReceiveMemoryWarning 的默认实现卸载视图后重新加载视图时,不会被调用。我不知道为什么它在应用程序启动时会有所不同,但我确实有一个解决方法

我所做的是将一个名为 unloadedByMemoryWarning 的布尔 ivar 设置为 YES didReceiveMemoryWarning,如下所示:

- (void) didReceiveMemoryWarning {
    unloadedByMemoryWarning = YES;
    [super didReceiveMemoryWarning];
}

然后,在 viewDidLoad 中,如果该标志为 true,我将其设置为 NO,然后自己调用旋转方法:

if (unloadedByMemoryWarning) {
    unloadedByMemoryWarning = NO;
    [self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
    [self willAnimateRotationToInterfaceOrientation:self.interfaceOrientation duration:0];
    [self didRotateFromInterfaceOrientation:self.interfaceOrientation];
}

我必须这样做有点糟糕,但它确实有效,现在我不太担心因为使用太多内存而被 iOS 杀死。

The answer, determined thanks to pointers from dbarker, is that the view controller's rotation methods, including -willRotateToInterfaceOrientation:duration: and -willAnimateRotationToInterfaceOrientation:duration:, will not be called when a view is reloaded after a the default implementation of didReceiveMemoryWarning has unloaded the view. I've no idea why it would be different on app launch, but I do have a workaround

What I did was to set a boolean ivar, named unloadedByMemoryWarning, to YES in didReceiveMemoryWarning, like so:

- (void) didReceiveMemoryWarning {
    unloadedByMemoryWarning = YES;
    [super didReceiveMemoryWarning];
}

Then, in viewDidLoad, if that flag is true, I set it to NO and then call the rotation methods myself:

if (unloadedByMemoryWarning) {
    unloadedByMemoryWarning = NO;
    [self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
    [self willAnimateRotationToInterfaceOrientation:self.interfaceOrientation duration:0];
    [self didRotateFromInterfaceOrientation:self.interfaceOrientation];
}

Kinda sucks that I have to do this, but it does work, and now I'm less concerned about getting killed by iOS for using too much memory.

薄暮涼年 2024-10-18 05:52:13
  1. 我认为 iOS 5 可能会解决这个问题。

  2. 对于 iOS 4.3,我很幸运地进行了另一次修复。从笔尖加载后:

    [parent.view addSubview:nibView];  
    nibView.frame =parent.view.frame;  
    [nibView setNeedsLayout];  
    

    如果有效,您可以放弃 unloadedByMemoryWarning 逻辑,因为每次加载都是安全的。得到了提示&代码(基本上)来自此处

  1. I think iOS 5 might fix this.

  2. For iOS 4.3, I've had good luck with another fix. After the load from nib:

    [parent.view addSubview:nibView];  
    nibView.frame = parent.view.frame;  
    [nibView setNeedsLayout];  
    

    If that worked, you could jettison the unloadedByMemoryWarning logic, since it's safe to do every load. Got the tip & code (basically) from here.

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