2012-11-01 21 views
0

我有兩個xib文件。我從「ViewController.xib」開始,當用戶點擊一個按鈕時,它會加載一個名爲「GamePage.xib」的不同xib文件。在第二個xib問題中加載viewDidLoad

按鈕的代碼是:

-(IBAction)startButtonClicked{  
    UIViewController *gamePage = [[UIViewController alloc] initWithNibName:@"GamePage" bundle:nil]; 
    [self presentViewController:gamePage animated:NO completion:nil]; 
} 

當點擊該按鈕時,第二XIB顯示在屏幕上,但它的「viewDidLoad中」法不跑......

這是爲什麼?

+0

? – MasterRazer

回答

2
UIViewController *gamePage = [[UIViewController alloc] initWithNibName:@"GamePage" bundle:nil]; 

這看起來不正確。

viewDidLoad可能正在運行,但它是蘋果的UIViewControllerviewDidLoad。初始化時,您應該使用您的班級而不是UIViewController。一般來說,您應該使用您需要調用的viewDidLoad類。

+0

完美!謝謝 –

1

您的XIB必須擁有其所有者。你需要創建一個派生自UIViewController的類,然後你需要讓你的XIB的類擁有者。 只要按照下列步驟操作:

  • 打開你的項目
  • 在左側導航面板
  • 按右鍵選擇項目名稱並選擇新建文件
  • 選擇目標C型類
  • 當你點擊在它上面,它會要求你給它起個名字
  • 假設你給了GameViewController
  • 檢查「With XIB for Inte rface」
  • 然後添加文件
  • 現在你的導航面板中,你會看到三個類 GameViewController.h GameViewController.m GameViewController.xib 在廈門國際銀行創建的接口。

現在,如果你必須移動到這個類,並顯示其看來,寫代碼如下:什麼是你的viewDidLoad第二視圖代碼

-(IBAction)startButtonClicked{  
    GameViewController *gamePage = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil]; 
    [[self navigationController] pushVIewController:gamePage animated:YES]; 
    [gamePage release]; //If not used ARC 
}