loadView vs viewDidLoad

来源:互联网 发布:sql ndf 数据库还原 编辑:程序博客网 时间:2024/05/29 06:42


The Problem:
I was getting a white screen with no data in a UITableView on it in the iPhone simulator.

The Solution:
I had my initialization code for the array in the the "loadView" method and not the "viewDidLoad" method

Explanation:
Don't read self.view in -loadView. Only set it, don't get it.The self.view property accessor calls -loadView if the view isn't currently loaded. There's your infinite recursion. I'm guessing UITableView calls a View [pretty good guess since "view" is in the name. :)] which in turn caused my recursion.

This was a stupid little error that caused me about 30 minutes of my life due to the fact I didn't get any build errors. A simple copy and paste moved me forward.

Update: I think it might be important to distinguish the differece between loadView and viewDidLoad. (below)

loadView is the method in UIViewController that will actually load up the view and assign it to the "view" property. This is also the location that a subclass of UIViewController would override if you wanted to programatically set up the "view" property.viewDidLoad is the method that is called once the view has been loaded. This is called after loadView is called. It is a place where you can override and insert code that does further initial setup of the view once it has been loaded.

 

 

 

 

Don't read self.view in -loadView. Only set it, don't get it.

--前一陣我也經常犯了這個錯誤,導致死循環,因為只要一get view,而view又還沒創建,框架就會調用loadView來嘗試創建view

 

原创粉丝点击