當您將其view
設置爲UIViewController
時,如果該視圖尚未實例化(這是您第一次提出要求),則需要創建它(°)。
當一個視圖控制器需要實例其觀點:如果一個XIB與您UIViewController
相關
- ,則畫面從XIB加載。這意味着XIB文件是非存檔的,並且XIB中描述的所有對象都被分配/實例化。 XIB中的每個視圖因此通過使用
alloc
+ initWithCoder:
+ autorelease
(NSCoder
是解壓縮XIB的解碼器)來實例化。因此,您的每個意見都會收到initWithCoder:
消息
- 如果沒有你
UIViewController
有關廈門國際銀行,它會調用其loadView
方法,讓您以編程方式創建視圖。在這種情況下,你在那裏編寫代碼來創建你的視圖,如[[[UIView alloc] initWithFrame:...] autorelease]
。因此,您的視圖將通過代碼實例化並接收initWithFrame:
消息,而不是initWithCoder:
方法。
- 其中
UIViewController
視圖已經或者通過取消歸檔XIB文件或通過在loadView
代碼創建和實例化,所述視圖被加載和UIViewController
接收viewDidLoad
消息。
所以每個視圖收到任一initWithFrame:
或initWithCoder:
(如果來自XIB創建)消息(如果由代碼創建)第一,然後一旦UIViewController
的view
創建的所有視圖的層次結構,所述UIViewController
本身收到viewDidLoad
消息。這總是按照這個順序。
@implementation YourCustonUIView
- (id)initWithCoder:(NSCoder*)decoder
{
// here the view is being allocated from the XIB. "alloc" as been called, "initWithCoder" is in progress
// frame is not set at this line, but as soon as we call the super implementation…
self = [super initWithCoder:decoder];
if (self)
{
// Here all the properties of the view set in the XIB file (thru the inspector) are now applied
// Including the frame of the view (and its backgroundColor and all what you set thru IB)
// so self.frame is initialized with the correct value here
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
// here the view is being allocated from code. "alloc" as been called, "initWithFrame" is in progress
// self.frame is not set at this line, but as soon as we call the super implementation…
self = [super initWithFrame:frame];
if (self)
{
// Here the view is initialized with its frame property
// so self.frame is initialized with the correct value here
// and you can initialize every other property from here
}
return self;
}
@end
@implementation YourCustomUIViewController
-(void)viewDidLoad
{
// At that point, the view of your UIViewController has been loaded/created
// (either via XIB or via the loadView method), so either its initWithCoder (if via XIB)
// or its initWithFrame (if via loadView) has been called
// so in either case, its frame has the right value
}
/*
// In case your YourCustomUIViewController does not have a XIB associated with it
// You have to implement this method to provide a view to your UIViewController by code
-(void)loadView
{
// Create a view by code, and give it a frame
UIView* rootView = [[UIView alloc] initWithFrame:CGRectMake(0,0,..., ...)];
rootView.backgroundColor = [UIColor redColor]; // for example
self.view = rootView;
[rootView release];
// at the end of this method, self.view must be non-nil of course.
}
*/
@end
(°),另一種可能性是,該觀點已被實例化,但後來解除了分配的,因爲你收到了內存不足的警告,而你的觀點是在屏幕上不那麼所使用的內存視圖已被回收。但無論如何,該視圖因此尚未加載並且必須被加載/創建
或者您可以繼承UIView並使用layoutSubviews。 – Rakesh
如果我爲我的子類UIViewController創建一個initWitFrame方法,是否有一個「內置」的方式,該控制器的視圖用該框架初始化?我不這麼認爲,但視圖框架是如何初始化的?現在,我從法拉爾的方法調用中保存了幀,並在viewDidLoad中設置了視圖幀。我認爲這有點馬虎。 – Jim
根據你的自定義'initWithFrame:'方法,你正在初始化視圖控制器,然後在分配視圖控制器的視圖之前設置視圖的框架(它並不指向任何特定的視圖)。所以你應該先設置視圖,然後設置框架。 – Rakesh