是否可以在内存警告时手动弹出当前不可见的视图控制器(不同选项卡中的应用程序)?
我有一个“内存管理与用户体验”或者只是一个愚蠢的问题。让我们想象一个基于 UITabBarController
的应用程序,带有两个选项卡。当用户位于第一个选项卡时,内存警告到达,第二个选项卡的视图控制器处理 didReceiveMemoryWarning
。我们还假设第二个选项卡有一个推送视图控制器。问题:
- 在第二个中手动弹出VC可以吗 选项卡与
popViewControllerAnimated:
当 发出内存警告? - 它被认为是糟糕的用户体验还是糟糕的 记忆管理的想法?我认为用户可能 对他/她所看到的感到惊讶 切换回第二个选项卡后,但是 如果我不弹出那个 VC,用户就会 只看到一个空白屏幕。如果用户点击“返回”,第二个选项卡 VC 无论如何都会重新加载自身(使用
viewDidLoad:
重新启动生命周期,这比应用程序被 iOS 杀死要好)。
我在上述方法中看到的唯一缺陷是当我推送的 VC 也推送了一些 VC 时。然后,代码就会变得复杂……如果有其他东西被推动,它就会进一步复杂化,让我很难维护这样的意大利面条代码:
- (void)didReceiveMemoryWarning {
UIViewController *pushedController = [self.navigationController visibleViewController];
if ([pushedController isKindOfClass:[MyController class]]) {
// POP
[self.navigationController popViewControllerAnimated:NO];
} else {
// MyController pushed something
UIViewController *innerController = [pushedController.navigationController visibleViewController];
if ([innerController isKindOfClass:[MyOtherController class]]) {
[innerController.navigationController popViewControllerAnimated:NO];
// Final POP
[self.navigationController popViewControllerAnimated:NO];
}
}
[super didReceiveMemoryWarning];
}
你的方法/建议是什么?也许已经有一个简单的方法来解决这个问题,但我忽略了它?
I have a "memory management vs user experience" or just a silly question. Let's imagine a UITabBarController
based app with two tabs. While user is in 1st tab, a memory warnings arrives and 2nd tab's view controller handles didReceiveMemoryWarning
. Let's also assume that 2nd tab has a pushed view controller. Questions:
- is it OK to manually pop VC in 2nd
tab withpopViewControllerAnimated:
when
memory warning is issued? - Is it considered a bad UX or bad
mem-mgmt idea? I think the user may
be surprised with what he/she sees
after switching back to 2nd tab, but
if I don't pop that VC the user will
see just a blank screen. If user taps 'Back', 2nd tab VC will reload itself anyway (restart life-cycle withviewDidLoad:
and it's better than the app being killed by iOS).
The only flaw I see in above approach is when my pushed VC also pushed some VC. Then, the code would complicate... and if there was anything else pushed, it would complicate further leaving me with hard to maintain spaghetti code like that:
- (void)didReceiveMemoryWarning {
UIViewController *pushedController = [self.navigationController visibleViewController];
if ([pushedController isKindOfClass:[MyController class]]) {
// POP
[self.navigationController popViewControllerAnimated:NO];
} else {
// MyController pushed something
UIViewController *innerController = [pushedController.navigationController visibleViewController];
if ([innerController isKindOfClass:[MyOtherController class]]) {
[innerController.navigationController popViewControllerAnimated:NO];
// Final POP
[self.navigationController popViewControllerAnimated:NO];
}
}
[super didReceiveMemoryWarning];
}
What's your approach/advice? Maybe there's already a simple approach for this and I overlooked it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何未定义的行为肯定意味着糟糕的用户体验。当您的控制器收到内存警告时,最好的方法是释放所有缓存的图像、视图(不在视图中)、变量(未使用)。您可能希望以这样的方式设计应用程序,即上述场景中的内存管理不会导致未定义的行为。您的应用程序可能不想占用所有内存并且不知道如何释放它。
它应该能够释放不需要的内存并处理警告。您可能需要指定您所持有的数据类型,以便在此处获得更具体的响应。
Any undefined behaviour would surely mean bad user experience. When your controller gets a memory warning the best approach would be to release any cached images, views(which are not in view), variables(not in use). You may want to design the app in such a way that memory management in the above scenario would not lead to undefined behaviour. Your app may not want to hog up all the memory and not know how to release it.
It should be able to release the unwanted memory and handle the warning. You may want to specify what kind of data you are holding inorder to get a more specific response here.