2013-02-11 76 views
0

我有一個嵌入在RootViewController中的導航控制器。存在於RootViewController中的元素是一個分段控件和一個UIView。當選擇不同的段時,UIView將顯示相應的ViewController。如何使pushViewController:在以下情況下工作?

現在,顯示在UIView中的每個ViewController都是一個UITableViewController。

我需要根據段選擇顯示錶值。此外,如果選擇了表格的任何一行,則必須使用詳細視圖移動到更新的ViewController。

我能夠根據細分的變化顯示UITableViewControllers。然而,當我點擊表格的行時,即使我已經在故事板中創建了特定的ViewController並設置了一個標識符,出現的較新的ViewController也是空白的(黑屏)。

代碼在ViewController.m分段的控制變化

- (void)segmentedControlChangedValue:(UISegmentedControl*)segmentedControl { 

int selIndex = segmentedControl.selectedIndex; 

if (selIndex == 0) 
{ 
    aTable = [[aTableViewController alloc] init]; 

    aTable.view.frame = self.myView.bounds; 
    [self.myView addSubview:aTable.view]; 
    [self addChildViewController:aTable]; 
} 

if (selIndex == 1) 
{ 
    bTable = [[bTableViewController alloc] init]; 

    bTable.view.frame = self.myView.bounds; 
    [self.myView addSubview:bTable.view]; 
    [self addChildViewController:bTable]; 

} 

if (selIndex == 2) 
{ 
    //NSLog (@"Well you selected a 3rd option"); 
}} 

代碼爲didSelectRowAtIndexPath方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
[super tableView:tableView didSelectRowAtIndexPath:indexPath]; 

newDetailController* newController = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"]; 

newController = [[newDetailController alloc] init]; 

[[self navigationController] pushViewController:newController animated:YES];} 

的pushViewController推給我一個空的觀點,即使我已指定在Storyboard設計中使用ViewController並使用標識符。

請幫我理解我必須採取什麼措施來糾正這個錯誤。

UPDATE - 上述問題已解決。

我現在面臨的問題是無法訪問newViewController中的數據。

我的數據傳遞給newViewController如下:

  1. 創建一個名爲的NewClass類,其中包含所有我想通過跨越的元素。

  2. 在firstViewController中創建一個NSMutableArray「newObjects」。

  3. 在此方法中創建每個newClass對象。

    • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    static NSString *CellIdentifier = @"Cell";

    customBigTableCell *cell = (customBigTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) { cell = [[customBigTableCell alloc] init]; }

    cell.name.text = [object objectForKey:@"objName"];

    newClass* newObj = [[newClass alloc] init];

    newObj.objName = cell.name.text;

    [newObjects addObject:newObj];

    return cell; } 請原諒我..因爲與代碼塊的縮進是搞砸了...... 另外PFObject是COS我使用數據庫的解析。

  4. 將創建的對象添加到newObjects數組。

  5. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法,這是代碼

    • (無效)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath { [超級的tableView:的tableView didSelectRowAtIndexPath方法:indexPath];

      UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@「MainStoryboard」bundle:[NSBundle mainBundle]];

      newDetailController * newController = [storyboard instantiateViewControllerWithIdentifier:@「UpcomingDetail」];

      obj = [[newClass alloc] init];

      NSIndexPath * path = [self.tableView indexPathForSelectedRow];

      obj = [newObjects objectAtIndex:path.row]; (@「Row selected is%ld」,(long)path.row); //這將返回選中的正確行

      NSLog(@「Movie selected is%@」,[[newObjects objectAtIndex:path.row] objName]); //然而,根據cellForRowAtIndexPath方法,這不是正確的。

      [newController setNewObj:obj];

      [[self navigationController] pushViewController:newController animated:YES]; }

當運行該代碼,該行被正確地選擇。但是我沒有得到數據庫中的相應行。數組與冗餘數據一起存儲。

回答

1

你需要擺脫中線:

newController = [[newDetailController alloc] init]; 

這只是重新定義newController(如無視圖建立一個空的控制器),您在第一行定義。

+0

謝謝@rdelmar。但是,當我刪除該語句,我得到以下錯誤... 應用程序試圖推動一個無視圖控制器的目標 AJoseph 2013-02-12 08:10:24

+0

此外,我檢查了類似的問題,他們問我不要重寫 - (void)loadView方法。但是,我沒有在我的程序中重寫該方法。 – AJoseph 2013-02-12 08:14:56

+0

@AJoseph,如果你得到這個錯誤,那麼出於某種原因,上面的行不返回視圖控制器。你應該記錄newController和self.storyboard來查看哪一個是零。此外,你可能不應該重載loadView - 你爲什麼這樣做? – rdelmar 2013-02-12 15:32:49

相關問題