2012-04-30 92 views
0

我是帶有Objective C和XCode 4.2的iphone開發新手。現在我成功實例化了一個UITableViewController,它可以遍歷一個多級json數據樹。不過,我無法爲葉子頁面建立用戶界面。讓我告訴你的代碼片段至今:iphone uiviewcontroller不顯示?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
     int row = [indexPath row]; 
     NSString * currentrow = [NSString stringWithFormat:@"%d.",row]; 


     NSDictionary * item = [dataArray objectForKey:currentrow]; 
     NSDictionary * children = [item objectForKey:@"children"]; 
     if(children != nil) 
     { 
      uinavTableViewController *childView = [[uinavTableViewController alloc] init]; 
      childView.dataArray = children; 
      [self.navigationController pushViewController:childView animated:YES]; 
     } 
     else 
     { 
      uinavMenuItem * childView = [[uinavMenuItem alloc] init]; 
      //uinavSecondViewController * childView = [[uinavSecondViewController alloc] init]; 
      [self.navigationController pushViewController:childView animated:YES]; 
     } 
} 

所以第一部分的if語句會檢查是否有子節點,如果是創建另一個uinavTableViewController。這部分工作完美。

else語句使用uinavMenuItem創建葉頁。當我在模擬器中運行它時,葉頁只是一個黑屏。我看着我的NSLog,可以確認uinavMenuItem viewDidLoad確實被解僱了。所以控制器代碼工作正常,但我只是不知道如何鏈接到我在故事板中繪製的視圖。

我在做什麼錯,可防止uinavMenuItem從顯示我在故事板已經開?

回答

2

當您想要初始化您在故事板中製作的視圖控制器時,請使用UIStoryboard-instantiateViewControllerWithIdentifier。您可以在視圖控制器屬性下的故事板中定義此標識符。您的視圖已加載,但爲空,因爲它沒有加載故事板中的內容。這可能是因爲找不到一個而調用-loadView

在這裏你輸入的標識符:

Enter the identifier here

+0

謝謝,你能否詳細說明「在視圖控制器屬性下的故事板中定義標識符」?我正在瀏覽Xcode面板,但無法找到類似於instantiateViewContollerWithIdentifier – John

+0

添加的圖像以清晰起見。 – Morrowless

+0

好吧,我做了標識符。然後我用這個替換我的else語句:'uinavMenuItem * childView = [self.storyboard instantiateViewControllerWithIdentifier:@「menuleafsegue」]; [self.navigationController pushViewController:childView animated:YES];'是否沿着正確的路線?現在模擬器不會去我的uinavMenuItem。 – John

0

如果您使用的故事板,它更容易只是創建了一個的UITableViewController SEGUE到葉控制器。您可以通過按住Control鍵並點擊表格視圖控制器然後將push segue拖到葉子控制器來完成。在X-Code側欄中,給出segue和標識符。

然後在你的代碼,而不是調用[self.navigationController pushViewController]你可以使用:

if(children != nil) 
{ 
    [self performSegueWithIdentifier: @"YourIdentifierHere" sender: self]; 
} 
else 
{ 
    [self [performSegueWithIdentifier: @"TheOtherIdentifier" sender: self]; 
} 

將數據傳遞到uinavTableViewController,您使用prepareForSegue方法在執行SEGUE相同的控制器:

- (void)prepareForSegue: (UIStoryboardSegue *)segue sender: (id)sender 
{ 
    // Because you have two segues, you need to check the identifier 
    if ([[segue identifier] isEqualToString: @"YourIdentifierHere"]) 
    { 
     uinavTableViewController childView = (uinavTableViewController *)segue.destinationViewController; 
     childView.dataArray = children; 
    } 
} 
+0

感謝,我想不通HOWTO給SEGUE的標識符。我嘗試按Ctrl +點擊xcode的右邊欄中的幾個行事,但我沒有注意到任何與segue標識符明顯有關的東西 – John

+0

首先按住Control鍵並點擊表格視圖控制器,然後在「Storyboard Segues」下拖動「推「到葉控制器。故事板將在控制器之間創建一個箭頭(這是繼續)。點擊箭頭(或箭頭中間的圖標),然後打開屬性檢查器(XCode的右側側欄,從左側開始的第四個標籤,看起來像一個皮帶扣),然後編輯「標識符」。 –

+0

謝謝。現在,當我運行模擬器時,出現SIGABRT錯誤。所以我想這是一個內存錯誤,或者我沒有正確實例化某些事物? – John

相關問題