我有一个基于导航栏的应用程序,在 UIViewController 中,我在选择照片时收到内存警告。它已处理,但是当我弹出视图控制器并返回到顶层视图控制器时,导航栏为空白。其他所有内容都会重新加载,但导航栏按钮和标题不会。
我知道 viewDidUnload 是在顶级视图控制器上调用的,当我导航回它时,VC 会使用 viewDidLoad 再次重新加载,但导航栏没有加载到那里,它来自 mainwindow.xib。有什么想法我做错了吗? (OS 3.2,iPad)
编辑这是控制台日志的摘录,显示内存警告时发生的情况:
09:55:25.864 -[PhotoBox setPhotoImage:] size {1536, 2048}
09:55:26.169 -[PhotoBox setPhotoImage:] resized to {768, 1024}
09:55:26.180 Received memory warning. Level=1
09:55:26.236 ClockMakerVC: viewDidUnload
09:55:26.240 -[ClockMakerVC didReceiveMemoryWarning]
09:55:26.271 saving 1 state objs to /var/mobile/Applications/82F2C13B-8663-4241-A603-B2BAD29691C1/Documents/ViewItem_State_Data
09:55:26.298 -[ItemSettingsVC didReceiveMemoryWarning]
此时,我点击导航栏后退箭头返回到父视图控制器,这会导致其上发生 viewDidLoad (因为它之前已被卸载):
09:55:44.477 read 1 state objs from /var/mobile/Applications/82F2C13B-8663-4241-A603-B2BAD29691C1/Documents/ViewItem_State_Data
09:55:44.479 loadSavedItems: 295376119.689 opts=0 np=2 P=(284,402,200,200) L=(412,274,200,200) 1 PhotoBox
09:55:44.484 ClockMakerVC: viewDidLoad: 1 saved items loaded
I have an navigation bar based app and while in a UIViewController I get a memory warning while selecting a photo. It is handled but when I pop the viewcontroller and go back to the top level viewcontroller the navigation bar is blank. Everything else gets reloaded but not the navigation bar buttons and title.
I know that viewDidUnload was called on the top level view controller and when I navigate back to it the VC gets reloaded again with viewDidLoad but the navigation bar is not loaded there, it comes from the mainwindow.xib. Any ideas what I'm doing wrong? (OS 3.2, iPad)
EDIT Here's an excerpt from the console log showing what's happening at the time of the memory warning:
09:55:25.864 -[PhotoBox setPhotoImage:] size {1536, 2048}
09:55:26.169 -[PhotoBox setPhotoImage:] resized to {768, 1024}
09:55:26.180 Received memory warning. Level=1
09:55:26.236 ClockMakerVC: viewDidUnload
09:55:26.240 -[ClockMakerVC didReceiveMemoryWarning]
09:55:26.271 saving 1 state objs to /var/mobile/Applications/82F2C13B-8663-4241-A603-B2BAD29691C1/Documents/ViewItem_State_Data
09:55:26.298 -[ItemSettingsVC didReceiveMemoryWarning]
At this point I hit the navigation bar back arrow to return to the parent view controller, which causes a viewDidLoad on it (since it was previously unloaded):
09:55:44.477 read 1 state objs from /var/mobile/Applications/82F2C13B-8663-4241-A603-B2BAD29691C1/Documents/ViewItem_State_Data
09:55:44.479 loadSavedItems: 295376119.689 opts=0 np=2 P=(284,402,200,200) L=(412,274,200,200) 1 PhotoBox
09:55:44.484 ClockMakerVC: viewDidLoad: 1 saved items loaded
发布评论
评论(1)
根据 文档 viewDidLoad 是“...用于对从 nib 文件加载的视图执行额外的初始化步骤。”
我一直认为 viewDidLoad 只被调用一次,直到最近才意识到它是如何在内存警告后(在视图被卸载然后重新加载之后)再次被调用的。
第二次调用时,viewDidLoad 中主视图控制器的部分初始化代码无法正常工作。我修复了它的一部分并将其部分移动到 awakeFromNib 并解决了问题。
According to the documentation viewDidLoad is "...used to perform additional initialization steps on views that are loaded from nib files."
I had always thought that viewDidLoad was only called once and only recently realized how it is called again after a memory warning (after a view is unloaded and then later reloaded).
Parts of my main view controller's init code in viewDidLoad didn't work quite right when called a second time. I fixed parts of it and move parts of it to awakeFromNib and that solved the problem.