2013-01-22 29 views
2

我在UIViewController內有UITableViewController。雖然這個表視圖控制器是唯一涉及到的,但當用戶點擊一行時,它只是推送視圖。然而,自從我將它移動到UIViewController中的兩個之後,行的突然突然沒有任何作用。如何顯示包含我的UIViewController的UITableViewController的新視圖?

我已經tried searching around我不是第一個遇到這個問題,但沒有答案符合我的情況或者問題沒有工作答案。該鏈接是我發現的最接近的,但我不使用故事板 - 我使用單獨的XIB。

那麼如何從視圖控制器中的視圖控制器推新視圖?

要回顧一下:

  1. 這裏是我有什麼,它採取用戶到一個新的屏幕工作得很好!

    // Normal table behavior, as illustrated by [another question][2]. 
    
    - (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    
        SomeView *detailViewController = [[SomeView alloc] initWithNibName:@"SomeView" bundle:nil]; 
    
        // Pass the selected object to the new view controller. 
        [self.navigationController pushViewController:detailViewController animated:YES]; 
    } 
    
  2. 現在我有視圖 - 控制在一個視圖中的屬性 - 與上面的代碼,這對於tableviewcontroller,而不是在「主」視圖中的文件,不會導致新的屏幕再次出現!


感謝您的意見!這裏有一些代碼來闡明我的場景。

  1. 控制器內的控制器。這是我用來測試概念的測試項目中的一個文件。在這種情況下,我有一個tableview控制器內的tableview控制器。

    @interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
    
    // This is the controller within the controller 
    @property IBOutlet SecondTableViewController *secondTableController; 
    @property IBOutlet UITableView *secondTable; 
    
  2. SecondTableViewController有這個有趣的一點。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        // Navigation logic may go here. Create and push another view controller. 
    
        UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"SimpleNonTableViewController" bundle:nil]; 
        // ... 
        // Pass the selected object to the new view controller. 
        [manualViewControllerParent.navigationController pushViewController:detailViewController animated:YES]; 
    } 
    

用戶與被掛接到SimpleTableViewController相互作用的圖。這樣,SecondTableViewController就是「在」SimpleTableViewController之內。如果您想了解更多詳情,請隨時發表評論!


我把我的測試/概念項目放在github上。 https://github.com/hyliandanny/TableViewCeption

+1

您是否試圖推送已經被推送過的視圖控制器? – rooster117

+0

你是什麼意思「在一個UIViewController中?它是一個子視圖控制器嗎? – rdelmar

+0

@ rooster117在這種情況下,不是,現在顯示的屏幕是唯一通過這個表達到的 – Danny

回答

3

您需要使用自定義容器控制器來執行所需操作。如果你使用故事板,這將是最簡單的,但你也可以用xib代碼來完成它。外部控制器應該是一個UIViewController,而不是一個表視圖控制器。你可以這樣做(在外部控制器中):

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"SimpleNonTableViewController" bundle:nil]; 
    [self addChildViewController:detailViewController]; 
    detailViewController.view.frame = set the frame to what you want; 
    [self.view addSubview:detailViewController.view]; 
    [detailViewController didMoveToParentViewController:self]; 
} 

你應該閱讀關於自定義容器控制器的Apple文檔。

+0

自定義容器控制器,呃?很高興知道!我會檢查一下,謝謝你指出我正確的方向! – Danny

+0

我討厭這樣做給未來的研究人員,但我把這個答案標記爲「答案」,即使我以另一種方式解決了這個問題。這背後的原因是自定義容器控制器是一個反映我實際使用的iOS 5.0+解決方案:委託和協議。通過定義可由「外部」視圖控制器實現的協議方法,「內部」視圖控制器最終調用包含/外部視圖控制器處理的方法。從我讀到的有關Custom Container Controllers的內容來看,這與rdelmar暗示的思路是一樣的。 – Danny

0

你需要確保:

  • UITableView委託掛接到控制器。否則它不會撥打didSelectRow。你可以在xib或viewDidLoad方法中做到這一點。
  • 你self.navigationController不是零
  • 你detailViewController不是零

我也認爲你的意思是你有你的UITableView裏面UIViewControllerUITableView只是視圖,而UITableViewController是一個控制器。您不能在另一個控制器內部安裝控制器。

+0

爲什麼我不能在另一個控制器內部安裝控制器? – Danny

+0

是的,您可以有一個控制器在另一個控制器內部,這就是爲什麼我在我的評論中詢問它是否是子視圖控制器。但OP的做法是錯誤的。 – rdelmar

+0

嘿,我*是* OP!我很好,被告知某些事情是錯誤的,我很想理解*爲什麼*這是錯誤的。例如,在單個屏幕上的兩個表格之間切換的正確方法是什麼?那是我最初解決的問題。你聽起來很有信心,所以我很高興聽到你的知識!再次感謝! – Danny

相關問題