我發現了一個奇怪的行爲,並想解釋我在做什麼說法,是錯誤的。的UIViewController與initWithNibName創建:包:或通過一個IBOutlet表現不同
在AppDelegate類一個新創建的WindowBased項目,我加入一個UIViewController的窗口。
我可以用兩種不同的方法:
- 帶一個IBOutlet。在IB中,我簡單地實例化了一個UIViewController,將它的類設置爲TestViewController並連接它(代碼的場景A)。
- 用代碼創建UIViewController(場景B)。
- (void)applicationDidFinishLaunching:(UIApplication *)application {
#define USE_IBOUTLET YES // Comment this line to switch to scenario B
#ifdef USE_IBOUTLET
// Scenario A
[window addSubview:theTestViewController.view];
[window makeKeyAndVisible];
#endif
#ifndef USE_IBOUTLET
// Scenario B
TestViewController *theTestViewControllerProgrammatically;
theTestViewControllerProgrammatically = [[TestViewController alloc] initWithNibName:nil bundle:nil];
// According to Apple: "It is a good idea to set the view's frame before adding it to a window.", so let's do it
[theTestViewControllerProgrammatically.view setFrame:[[UIScreen mainScreen] applicationFrame]];
[window addSubview:theTestViewControllerProgrammatically.view];
[window makeKeyAndVisible];
#endif
}
由於我沒有在IB中做任何定製的對象,我應該在這兩種情況下都有相同的行爲。
情景A,使用IBOutlet按預期工作。
但方案B具有以下問題:
- 的視圖不是在正確的位置(20個像素高,並且由所述狀態欄覆蓋)。
- 視圖無法正確調整大小(例如,嘗試切換「來電狀態」欄中的內容)
爲什麼?
項目的Zip文件在這裏,如果你想重現該問題:http://dl.dropbox.com/u/1899122/code/ProtoWindowBasedStrangeness.zip
正確。好的趕上:)我編輯了問題鏈接的問題和zip文件的內容。 – Guillaume 2010-02-24 16:04:46